php更新按钮只更新最后一条记录

时间:2017-06-08 12:20:01

标签: php

这是我的第一个PHP程序,我有点陷入更新按钮代码我觉得问题出在Driver_ID我尝试了很多不同的东西,但没有什么对我有用。我的问题是如何让Driver_ID使按钮工作? 在下面的代码中我只得到最后一行id。如果有人可以帮助我,我会非常感激。

<?php
include("dbconnection.php");

//Fetch the data from tables
$query="SELECT * FROM drivers JOIN users ON users.User_ID = drivers.User_ID";
$result = mysqli_query($connect,$query);
            while($row = mysqli_fetch_array($result))
  {
       echo "<tr>
    <td>".$row['Driver_ID']."</td>
    <td>" . $row['Driver_Name'] . "</td>
    <td>" . $row['User_Name'] . "</td>

            <td><p data-placement='top' data-toggle='tooltip' title='Edit'><button class='btn btn-primary btn-xs' data-title='Edit' data-toggle='modal' data-target='#edit'value='".$row['Driver_ID']."' name='editr'><span class='glyphicon glyphicon-pencil'></span></button></p></td>  

              <td><p data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn  btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete'><span class='glyphicon glyphicon-trash'></span></button></p></td> 
               </tr>";

  $id=$row['Driver_ID'];
            }

mysqli_close($connect);
?>

隐藏控件的html代码

  <input class="form-control " type="hidden" value="<?php echo  $id ?>"  name="driverid"  >

更新按钮的代码

 <?php

require("dbconnection.php");

if(isset($_POST['update']))
    // include Database connection file 
                {
    $driver_id=$_POST['driverid'];      
$driver_name = $_POST['txtname'];

    $user_id = $_POST['UserN'];

 $sql = "UPDATE drivers 

                  SET Driver_Name='$driver_name',User_ID='$user_id'
                  WHERE Driver_ID='$driver_id'";


if (mysqli_query($connect, $sql)) {
echo "Record updated successfully";
header('Refresh:5 ; url=drivers.php');
}
 else {
echo "Error updating record: " . mysqli_error($connect);
}
}

    mysqli_close($connect);
?>

1 个答案:

答案 0 :(得分:0)

使用这样的输入: 由于您需要所有结果,您需要将它们全部存储

你的主要while循环可能就像

    $r=null;
    while(){
    $r++;

   echo '<input class="form-control " type="hidden" value="<?php echo  $id ?>"  name="driverid[]"  >'

    }
    echo '<input type="hidden" value='".$r."' name="records">';

之后只需使用一些循环即可查看所有结果 然后在update / insert

$records=htmlentities($_POST['records'], ENT_QUOTES, "UTF-8");
$i=0;
while ($i <= $records){
$driverid=htmlentities($_POST['driverid'][], ENT_QUOTES, "UTF-8");

//your update/insert query here
$i++;
}

我希望这有帮助