简体   繁体   中英

PHP form updating multiple mysql rows

I have a Table I created in PHP with fields that need to be populated.

Here is my PHP form:

<td><input type="text" name="reg" value="<? echo $rows['reg']; ?>" readonly/></td>
<td><input type="text" name="driver" value="<? echo $rows['driver']; ?>" readonly/></td>
<td><input type="text" name="departure" value="<? echo $rows['departure']; ?>"/></td>
<th><input type="text" name="destination" /></th>
<th><input type="text" name="sleep" /></th>
<th><input type="text" name="date_loaded" value="<? echo $rows['date_loaded']; ?>"/></th>
<th><input type="text" name="arrival_date" value="<? echo $rows['arrival_date']; ?>"/></th>
<th><input type="text" name="client" value="<? echo $rows['client']; ?>"/></th>
<th><input type="text" name="status" value="<? echo $rows['status']; ?>"/></th>
<th><input type="text" name="notes" value="<? echo $rows['notes']; ?>"/></th>

Then I have the following code to update the information to MySQL DB

/Post reply drawn 
$client = $_POST['client'];
$reg = $_POST['reg'];
$driver = $_POST['driver'];
$departure = $_POST['departure'];
$destination = $_POST['destination'];
$sleep = $_POST['sleep'];
$date_loaded = $_POST['date_loaded'];
$arrival_date = $_POST['arrival_date'];
$status = $_POST['status'];
$notes = $_POST['notes'];

// Record to MySQL
// database settings

date_default_timezone_set('Africa/Johannesburg');
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d") ;
$time = date("H:i:s");

// Create connection
$conn = new mysqli($host, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO verification_hx SET
        client = '".$client."',
        reg = '".$reg."',
        driver = '".$driver."',
        departure = '".$departure."',
        destination = '".$destination."',
        sleep = '".$sleep."',
        date_loaded = '".$date_loaded."',
        arrival_date = '".$arrival_date."',
        status = '".$status."',
        notes = '".$notes."'";

if ($conn->query($sql) === TRUE) {
    echo "Vehicles has been Updated.  ";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    echo("Error description: " . mysqli_error($con));
    echo "Error in Case Management Table";
}

$sql1 = "UPDATE verification SET
        client = '".$client."',
        reg = '".$reg."',
        driver = '".$driver."',
        departure = '".$departure."',
        destination = '".$destination."',
        sleep = '".$sleep."',
        date_loaded = '".$date_loaded."',
        arrival_date = '".$arrival_date."',
        status = '".$status."',
        notes = '".$notes."'
        WHERE reg = .$reg.";

if ($conn->query($sql1) === TRUE) {
    echo "";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    echo "Error in Bureau Case File Table";
}

$conn->close();

Now my problem is it is not updating the form at all, and the new record recorded in the database only has reg and driver details all other details are left blank

Your problem is on PHP side

"WHERE reg = .$reg.";

It should be

"WHERE reg=\'{$reg}\'";

As you may find in PHP documentation, try to use DateTime classe instead of date function. You also should look for SQL injection before you publish your project, because users are able to do that inside your code, once you are accepting any string inserted by them with no treating.

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