Сообщение об ошибке обновления формы в MySQL, используя php

Я пытаюсь обновить форму базы данных mysql с помощью php, но когда я добавляю значения в поля ввода, они публикуются пустыми. Это ошибка:

Ошибка Сохранить [ОБНОВЛЕНИЕ Клиентов УСТАНОВЛЕНО Имя Фамилии = '', Surename = '', Отец = '', ID = '', AMKA = '', Адрес = '', 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."]";
}

?>

2 ответа

Ваша ошибка в том, что вы ошиблись в значениях полей таблицы. Проверьте ниже и замените код в разделе 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
$conn = new mysqli('localhost', 'root', '','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, City 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 if($list['City']==$row['City']){echo 'selected="selected"';} ?>><?php echo $list['City']; ?></option>                               
                            </option>
                        <?php } ?>
                    </option>
              </select>
    </table>
    <input type="submit" value="Update">
</form>

SavedRecord.php

<?php

$conn = new mysqli('localhost', 'root', '','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["Cities"];
$customerCode = $_GET["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's
            WHERE CustomerCode = '$customerCode'";

$updQuery = $conn->query($updData);

if($updQuery) {

    echo "Data Updated";
} else {

    echo "Error Save [".$updData."]";
}

?>

Этот код работает отлично. попробуйте и дайте мне знать, если с вами снова возникнут какие-либо препятствия.

Другие вопросы по тегам