简体   繁体   English

PHP表单更新多个mysql行

[英]PHP form updating multiple mysql rows

I have a Table I created in PHP with fields that need to be populated. 我有一个用PHP创建的表,其中的字段需要填充。

Here is my PHP form: 这是我的PHP表格:

<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 然后我有以下代码将信息更新到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 现在我的问题是根本不更新表单,并且数据库中记录的新记录仅包含reg和driver详细信息,所有其他详细信息都留为空白

Your problem is on PHP side 您的问题是在PHP方面

"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. 您可能会在PHP文档中找到,请尝试使用DateTime类代替日期函数。 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. 在发布项目之前,您还应该查找SQL注入,因为一旦您接受了他们插入的任何字符串,用户都可以在代码中进行SQL注入,而无需进行任何处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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