简体   繁体   中英

Post form update error to mysql using php

I'm trying to update a form to mysql database with php but when i add values to the input fields they're posted empty. This is the error:

Error Save [UPDATE Customers SET Forename = '', Surename = '', Father = '', ID = '', AMKA = '', Address = '', AddressNumber = '', PostCode = '', Area = '', City = '', WHERE CustomerCode = '4']

As you can see the GET for the customer code works fine but the POST is not working.

Here is my code for the edit form:

<?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>

And the 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."]";
}

?>

Your Error is you have misspelled the table field values. Have a check below and replace the code in the WITH Section

Replace

$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'";

With

$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'";

Here i will provide with the Exact output that is needed for you and i have tested it in my local-host and working fine.

Edit form Page:

<?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."]";
}

?>

This code works perfect. have it a try and let me know if any hurdles happens to you again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM