我正在编辑数据并更新它们,但数据不会更新。这是我的代码
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"; }
答案 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 .....");