只是尝试查询数据库以从Web UI更新行(及其中的数据)。 我有一张桌子"客户" - 只是想要更新记录(我已经实现了INSERT和DELETE)。
我正在回应该表并包含通过以下语法更新的选项:
<td><a href =Scripts/Update.php?id=".$row['Customer_ID'].">Update</a> </td>";
这显示&#34;更新&#34;就像我需要的那样,在每一行的末尾,当我点击它时,它会运行更新脚本。大。
虽然回显了表格,但我还将输入类型更改为文本,因此用户可以点击该文档,编辑数据并单击更新按钮。
然而问题是,当我尝试更新表中的记录时,它会刷新回所选的标题页但没有更新任何信息,只返回带有已清除数据单元格的行。 (它没有删除数据库中的行,因为我在表中显示了客户ID,但仍然存在)。
我的Update.php脚本如下:
<?php
include "../Connection.php";
$Firstname = mysqli_real_escape_string($con, $_POST['FirstName']);
$Lastname = mysqli_real_escape_string($con, $_POST['LastName']);
$CusEmail = mysqli_real_escape_string($con, $_POST['Email']);
$CusUsername = mysqli_real_escape_string($con, $_POST['Username']);
$CusPhone = mysqli_real_escape_string($con, $_POST['Phone']);
$CusCountry = mysqli_real_escape_string($con, $_POST['Country']);
$CusTown = mysqli_real_escape_string($con, $_POST['Town']);
$CusAddress = mysqli_real_escape_string($con, $_POST['Address']);
$CusPostcode = mysqli_real_escape_string($con, $_POST['Postcode']);
$sqlupdate = "UPDATE customer SET Customer_FirstName = '$Firstname', Customer_LName ='$Lastname', Customer_Email ='$CusEmail', Customer_Username ='$CusUsername', Customer_Phone ='$CusPhone', Customer_Country ='$CusCountry', Customer_Town ='$CusTown', Customer_Address = '$CusAddress', Customer_Postcode = '$CusPostcode' WHERE Customer_ID ='$GET_[id]'";
mysqli_query($con, $sqlupdate);
mysqli_close($con);
if($sqlupdate){
header('Location:../CustomerRecords.php');
}
else{
echo "Failed!";
}
我尝试了一些事情,例如,我通常使用POST方法从HTML表单执行此操作,而这样我就是通过echo a href ...(这是我第一次) )。我在DELETE函数上使用了相同的方法,效果很好。 接下来,我通常会添加:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
并且
if(isset($_POST)){
如果需要,但是再试一次,得到相同的结果。
可能是我的SQL语法错了,但我无法弄清楚,如果有人可以建议它会很棒。
谢谢。
更新:
我上面做了一些修改,我现在一直在盯着这段代码,只是&#34;玩&#34;现在用它...我也试过这个,但它会抛出错误,这看起来比现在更糟糕了:
$sqlupdate = "UPDATE customer SET Customer_FirstName = '$Firstname', Customer_LName ='$Lastname', Customer_Email ='$CusEmail', Customer_Username ='$CusUsername', Customer_Phone ='$CusPhone', Customer_Country ='$CusCountry', Customer_Town ='$CusTown', Customer_Address = '$CusAddress', Customer_Postcode = '$CusPostcode' WHERE Customer_ID ='$_GET[id]'";
if(mysqli_query($con, $sqlupdate)){
header('Location:../CustomerRecords.php');
}
else{
echo "Failed!";
}
答案 0 :(得分:0)
您实际上并未执行查询。后:
$sqlupdate = "UPDATE customer SET Customer_FirstName = '$Firstname', Customer_LName ='$Lastname', Customer_Email ='$CusEmail', Customer_Username ='$CusUsername', Customer_Phone ='$CusPhone', Customer_Country ='$CusCountry', Customer_Town ='$CusTown', Customer_Address = '$CusAddress', Customer_Postcode = '$CusPostcode' WHERE Customer_ID ='$GET[id]'";
您需要添加:
mysqli_query($con,$sqlupdate);