PHP MYSQL更新stament无法正常工作

时间:2016-08-23 20:41:24

标签: php mysql

此代码用于检查提交的表单值并更新表, 但它只是用空白替换字段

任何出错的想法,拜托?

<form action = "update.php" method = "POST">
    <p>
        New Name: <input type "text" name="name">
        <input type= "submit">
    </p>
</form>
<?php 

require ('/var/www/html/site1/connect_db.php');
if(!empty($_POST['name']) && !is_numeric($_POST['name']))
{
    $name=$_POST['name'];
    $name=mysqli_real_escape_string($dbc,$query);
    $name=strip_tags($name);


    #$query='update customers SET customerName = '".$name."' where customerNumber=114';
    $query = "update customers ". "SET customerName = $name"."where customerNumber=114" ;
    mysqli_query($dbc,$query);
}
else
{
    echo $name;
}

$query = 'select * from customers where customerNumber=103';
$result = mysqli_query($dbc,$query);

while ($row=mysqli_fetch_array($result, MYSQLI_NUM))
{
    echo"<p>Name : $row[1]</p>";
}
mysqli_close($dbc);

?>

1 个答案:

答案 0 :(得分:0)

您正在更新客户编号114,但选择103 out,其名称可能为空白。

您的更新语句需要在$ name位周围加上引号,如下所示:

$query = "UPDATE customers SET customerName = '$name' WHERE customerNumber=114";

编辑:请在问题评论中查看参数化查询建议。