如何在php中编辑数据使用$ _POST

时间:2017-09-11 16:42:31

标签: php mysql

我正在编辑数据并更新它们,但数据不会更新。这是我的代码

if(isset($_POST['submit'])){
        $tensanpham = $_POST['tensanpham'];
        $anh = $_POST['image'];
        $gia = $_POST['gia'];
        $giamgia = $_POST['giamgia'];
        $update = mysqli_query("UPDATE sanpham SET tensanpham='$tensanpham', image='$anh', gia='$gia', giamgia='$giamgia' WHERE id='$id'");
        if($update) {
            echo "update done";
        }
        else{
            echo "Fail"; }

2 个答案:

答案 0 :(得分:1)

您需要将MySQLi连接字符串传递给mysqli_query()函数:

$conn = mysqli_connect("localhost", "user", "password", "database_name");

if(isset($_POST['submit'])){
    $tensanpham = $_POST['tensanpham'];
    $anh = $_POST['image'];
    $gia = $_POST['gia'];
    $giamgia = $_POST['giamgia'];
    $update = mysqli_query($conn, "UPDATE sanpham SET tensanpham='$tensanpham', image='$anh', gia='$gia', giamgia='$giamgia' WHERE id='$id'");
    if($update) {
        echo "update done";
    } else {
        echo "Fail"; 
    }
}

你真的应该使用MySQLi预处理语句。您当前的代码存在SQL注入的风险。

答案 1 :(得分:0)

使用mysqli_query是错误的。应纠正为:

$mysqli_connection = mysqli_connect($DBHOST,$DBUSER,$DBPWD,$DBNAME);
$update = mysqli_query($mysqli_connection, "UPDATE .....");