我有一个使用用户输入的表单,并与数据库中的相同值进行比较,如果输入无效,则拒绝并且不更新。
当用户输入正确时,表单会转发它,我的代码会从该输入中找到相应的ID,然后更新它。
但到目前为止,它的作用是更新最后一条记录。
这是我的代码:
我道歉它有点长,所以请耐心等待。我仍然是学生和总菜鸟。
require("connect.php");
$query = mysql_query("SELECT * FROM driver LEFT JOIN vehicle ON driver.vehicle_id = vehicle.vehicle_id");
while($driverlist = mysql_fetch_array($query)){
$vehiclestatus = $driverlist['v_status'];
$driver = $driverlist['d_fname'];
$driver_id =$driverlist['driver_id'];
$v_model = $driverlist['v_model'];
$plate_number = $driverlist['plate_number'];
$vehicleid = $driverlist['vehicle_id'];
echo '<tr>';
echo '<td>'.$driver.'</td>';
echo '<td>'.$driver_id.'</td>';
echo '<td>'.$v_model.'</td>';
echo '<td>'.$plate_number.'</td>';
echo '</tr>';
}
if(isset($_POST['edit'])){
$vehicle_id = $_POST['vehicle_id'];
$driverid = $_POST['driver_id'];
require("connect.php");
mysql_query("UPDATE vehicle RIGHT JOIN driver ON vehicle.vehicle_id = driver.driver_id SET v_status = '1' WHERE driver.vehicle_id = '".$vehicle_id."' AND driver_id = '".$driverid."'")
or die(mysql_error());
}
if(isset($_POST['update']))
{
$vehicle_id = $_POST['vehicle_id'];
$driverid = $_POST['driver_id'];
require("connect.php");
mysql_query("UPDATE vehicle RIGHT JOIN driver ON vehicle.vehicle_id = driver.driver_id SET v_status = '0' WHERE driver.vehicle_id = '".$vehicle_id."' AND driver_id = '".$driverid."'")
or die(mysql_error());
header('Location: drivers.php');
}
if(!empty($_POST['registerbtn']))
{
$driver = $_POST['driver'];
require("connect.php");
$match_driver = mysql_query("SELECT * FROM driver WHERE d_fname = '$driver'");
$rows = mysql_num_rows($match_driver);
if($rows != 0)
{
while($row_driver = mysql_fetch_array($match_driver))
{
$a_fname = $row_driver[2];
$a_id = $row_driver[0];
}
}
error_reporting(0);
if($driver == $a_fname){
require("connect.php");
mysql_query("UPDATE trip_reservation SET driver_id = '".$a_id."' WHERE trip_id = '".$tripid."'");
//mysql_query("INSERT INTO trip_reservation (trip_id, driver_id) VALUES('".$tripid."', '".$a_id."') ON DUPLICATE KEY UPDATE trip_id = '".$tripid."'")or die(mysql_error());
echo "Driver Added Successfully";
}
else{
echo "The Driver is not found in the database <br>
Please input a valid Driver.";
}
}
else {
echo $registerform;
}
?>
答案 0 :(得分:0)
为什么在更新单个表上的记录时连接表。 其次,你在比较你甚至没有更新的表格上的WHERE条款
而不是这个
"UPDATE vehicle RIGHT JOIN driver ON vehicle.vehicle_id = driver.driver_id SET v_status = '0' WHERE driver.vehicle_id = '".$vehicle_id."' AND driver_id = '".$driverid."'"
这样做
"UPDATE vehicle SET v_status = '0' WHERE id = '".$vehicle_id."' AND driver_id = '".$driverid."'"
这假设您的车辆表有一个名为 id 的主键字段和一个名为 driver_id
的drivers_table的外键字段是的,它应该更新JUST ONE RECORD