gpt4 book ai didi

php - 使用php将表单更新错误发布到mysql

转载 作者:行者123 更新时间:2023-11-30 22:14:23 25 4
gpt4 key购买 nike

我正在尝试使用 php 将表单更新到 mysql 数据库,但是当我将值添加到输入字段时,它们显示为空。这是错误:

Error Save [UPDATE Customers SET Forename = '', Surename = '', Father = '', 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."]";
}

?>

最佳答案

您的错误是您拼错了表格字段值。检查下面并替换 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'";

关于php - 使用php将表单更新错误发布到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38945439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com