PHP表单更新多个mysql行

时间:2017-11-09 17:14:59

标签: php mysql

我在PHP中创建了一个表,其中包含需要填充的字段。

这是我的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>

然后我有以下代码将信息更新到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();

现在我的问题是它根本没有更新表单,而且数据库中记录的新记录只有reg和驱动程序详细信息所有其他详细信息都留空了

1 个答案:

答案 0 :(得分:0)

您的问题出在PHP方面

"WHERE reg = .$reg.";

应该是

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

正如您在PHP文档中所发现的那样,尝试使用DateTime classe而不是date函数。您还应该在发布项目之前查找SQL注入,因为一旦您接受由它们插入的任何字符串而没有处理,用户就可以在您的代码中执行此操作。