问题描述
我正在尝试通过使用PHP的查询来更新公司表,但数据库中没有更改。 MySQL数据库。
$pncon->query("UPDATE `company` SET
`name` = '" . $companyName . "',`parent_ID` = '" . $companyParent . "',`address` = '" . $address . "',`phone` = '" . $phone . "',`fax` = '" . $fax . "',`email` = '" . $email . "',`remarks` = '" . $remarks . "',`type` = '" . $system . "',`status` = '". $status ."'
WHERE `id` = '" . $id . "' ");
解决方法
您的查询看起来不错,但可能是错误的数据类型,无效的id或变量或违反了其他约束。
使用PDO语句会更好,因为它更安全。
您可以将代码修改为
$sql = "UPDATE `company` SET
`name` = '" . $companyName . "',`parent_ID` = '" . $companyParent . "',`address` = '" . $address . "',`phone` = '" . $phone . "',`fax` = '" . $fax . "',`email` = '" . $email . "',`remarks` = '" . $remarks . "',`type` = '" . $type . "',`status` = '" . $status . "'
WHERE `id` = '" . $id . "' " ;
$stmt = $pncon->prepare($sql); //prepare statement
$stmt->execute(); //execute the query
您可以找到PDO查询与执行here之间的区别。
,$pncon->query("UPDATE company SET
name = '$companyName',parent_ID = '$companyParent',address = '$address',phone = '$phone',fax = '$fax',email = '$email',remarks = '$remarks',type = '$system',status = '$status'
WHERE id = '$id'");