我正在尝试使用php将表单更新到mysql数据库,但是当我将值添加到输入字段时,它们将被发布为空。这是错误:
错误保存[UPDATE Customers SET Forename ='',Surename ='',父亲 ='',ID ='',AMKA ='',Address ='',AddressNumber ='',PostCode ='',Area ='',City ='',WHERE CustomerCode ='4']
正如您所看到的,客户代码的GET工作正常,但POST无效。
以下是编辑表单的代码:
<?php
$conn = new mysqli('localhost', 'root', 'password','erp');
if ($conn->connect_errno) {
die('Could not connect: ' . $conn->connect_error);
}
$id = $_GET['CustomerCode'];
$sql = $conn->query("SELECT Forename, Surename, FathersName, IDNumber, AMKA, Address, AddressNumber, PostCode, Area FROM Customers WHERE CustomerCode= '$id'");
$sqlList = $conn->query("SELECT City FROM Customers");
$row = $sql->fetch_array();
?>
<form action="SavedRecord.php?CustomerCode=<?php echo $id; ?>" method="post">
<table>
Name: <input type="text" name="Name" value="<?php echo $row['Forename']; ?>">
Surename: <input type="text" name="Surename" value="<?php echo $row['Surename']; ?>">
Father: <input type="text" name="Father" value="<?php echo $row['FathersName']; ?>">
ID: <input type="text" name="ID" value="<?php echo $row['IDNumber']; ?>">
AMKA: <input type="text" name="AMKA" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['AMKA']; ?>">
Address: <input type="text" name="Address" value="<?php echo $row['Address']; ?>">
Address Number: <input type="text" name="AddressNumber" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['AddressNumber']; ?>">
PostCode: <input type="text" name="PostCode" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['PostCode']; ?>">
Area: <input type="text" name="Area" value="<?php echo $row['Area']; ?>">
City: <select name="Cities">
<option>Select
<?php while($list = mysqli_fetch_array($sqlList)) { ?>
<option value="<?php echo $list['City']; ?>"><?php echo $list['City']; ?></option>
<?php if($list['City'] == $select) { echo $list['City']; } ?>
</option>
<?php } ?>
</option>
</select>
</table>
<input type="submit" value="Update">
</form>
更新表格:
<?php
$conn = new mysqli('localhost', 'root', 'password','erp');
if ($conn->connect_errno) {
die('Could not connect: ' . $conn->connect_error);
}
print_r($_POST);
$name = $_POST['Name'];
$surename = $_POST['Surename'];
$father = $_POST["Father"];
$id = $_POST["ID"];
$amka = $_POST["AMKA"];
$address = $_POST["Address"];
$addressNum = $_POST["AddressNumber"];
$postcode = $_POST["PostCode"];
$area = $_POST["Area"];
$city = $_POST["City"];
$customerCode = $_GET["CustomerCode"];
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
Father = '$father',
ID = '$id',
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city',
WHERE CustomerCode = '$customerCode'";
$updQuery = $conn->query($updData);
if($updQuery) {
echo "Data Updated";
} else {
echo "Error Save [".$updData."]";
}
?>
答案 0 :(得分:0)
您的错误是您拼写了表字段值。请在下方检查并替换WITH部分中的代码
<强>替换强>
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
Father = '$father',
ID = '$id', // here is your error the field name is not ID it is IDNumber
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city',
WHERE CustomerCode = '$customerCode'";
。通过强>
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
FathersName = '$father',
IDNumber = '$id',
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city',
WHERE CustomerCode = '$customerCode'";