【问题标题】:Empty query undefined variable when updating mysql Database更新mysql数据库时空查询未定义变量
【发布时间】:2014-08-01 19:28:28
【问题描述】:

我正在尝试设置一个可以更新我的产品的表单。 代码读取数据正常,但 $update 出现错误,导致更新无法执行任何操作。

错误是:
未定义变量:更新
mysqli::query(): 空查询(提交表单后)

请帮忙!谢谢。
//include database configuration file
    include("config.php");
    $mysqli->set_charset("utf8");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Edit Page</title>
</head>
<body>
<?php
    if(isset($_POST['Submit'])){//if the submit button is clicked

    $updateproductname = $_POST['updateproductname'];
    $updatesku = $_POST['productsku'];
    $updateproductoriginal = $_POST['updateoriginalname'];
    $updatedescshort = $_POST['updatedescshort'];

    $update = $mysqli->query("UPDATE testproducts". 
                    "SET product_sku=$updatesku, product_name=$updateproductname, 'product_originalname'='$updateproductoriginal', 'product_description_short='$updatedescshort' ".
                    "WHERE product_id = '$id' ");
    $mysqli->query($update) or die("Cannot update");//update or error
    }
?>
<?php
//Create a query
$sql = "SELECT * FROM testproducts WHERE product_id = $id";
//submit the query and capture the result
$result = $mysqli->query($sql) or die(mysql_error());
?>
<h2>Update Record <?php echo $id;?></h2>
<form action="" method="post">
<?php


    while ($row = $result->fetch_assoc()) {?>

<table border="0" cellspacing="10">
<tr>
<td>Product Name:</td> <td><input type="text" name="updateproductname" value="<?php echo $row['product_name']; ?>"></td>
</tr>
<tr>
<td>Product Original Name:</td> <td><input type="text" name="updateoriginalname" value="<?php echo $row['product_originalname']; ?>"></td>
</tr>
<tr>
<td>Product SKU:</td> <td><input type="text" name="productsku" value="<?php echo $row['product_sku']; ?>"></td>
</tr>
<tr>
<td>ShortDescription:</td> <td><input type="text" name="updatedescshort" size="100" value="<?php echo $row['product_description_short']; ?>"></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php   
}
    ?>
</form>
<?php
    if($update){//if the update worked

    echo "<b>Update successful!</b>";



}  
?>
</body>
</html>

【问题讨论】:

  • 查询中有错误。首先您执行 d 查询并将其存储在 $update 中。再次执行 $update 这不是查询但应该是资源。

标签: php database object mysqli


【解决方案1】:

a) 你很容易受到SQL injection attacks

b) 阅读mysqli_query() 的文档。该函数接受一个查询字符串,并返回一个结果句柄。然后,您将使用该结果句柄并尝试重新查询它。如果您为对所有 mysqli 调用进行适当的错误处理而烦恼,您就会看到这一点。

【讨论】:

    【解决方案2】:

    在将更新和选择代码移动到 html 顶部后能够更新记录

    <?php
        if(isset($_POST['Submit'])){//if the submit button is clicked
        // Check connection
    
        $productname = $_POST['updateproductname'];
        $productoriginal = $_POST['updateoriginalname'];
        $sku = $_POST['productsku'];
        $descshort = $_POST['updatedescshort'];
    
    
        $mysqli->query("UPDATE testproducts ".
                                "SET product_name='$productname',product_originalname='$productoriginal', product_sku='$sku', product_description_short='$descshort'".
    
                                " WHERE product_id='$id'");
    
        }
    ?>
    <?php
    //Create a query
    $sql = "SELECT * FROM testproducts WHERE product_id = $id";
    //submit the query and capture the result
    $result = $mysqli->query($sql) or die(mysql_error());
    //$query=getenv(QUERY_STRING);
    //parse_str($query);
    //$ud_title = $_POST['Title'];
    //$ud_pub = $_POST['Publisher'];
    //$ud_pubdate = $_POST['PublishDate'];
    //$ud_img = $_POST['Image'];
    $mysqli->close();
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      相关资源
      最近更新 更多