只更新最后一个数据库记录-需要数组


Only updating last database record - Array required?

有人可以帮助我,我只是新的PHP和SQL。我已经创建了一个数据库,并希望在HTML页面上使用表单更新它。除了它只更新最后一条记录外,一切都很好。我认为这是因为我需要一个数组,但不确定如何做到这一点。有没有人能举一些好的例子或者给我指出正确的方向?

代码如下:

显示页面

<?php
    $con=mysqli_connect("xx","xx","xx","xx");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM webquestion");
if ($result) {
   // create a new form and then put the results
   // into a table.
   echo "<form method='post' action='delete.php' >"; 
   echo "<table class='webquestion' >
    <tr>
   <th width='12%'>Department</th>
   <th width='15%'>Name</th>
   <th width='25%'>E-mail</th>
   <th width='20%'>Message</th>
   <th width='20%'>Notes</th>
   <th width='8%'>Delete</th>
   </tr>";

   while ($row = $result->fetch_object()) {
   $department = $row->department;
   $name = $row->name;
   $email = $row->email;
   $message = $row->message;
   $notes = $row->notes;
   $id = $row->id;
   //put each record into a new table row with a checkbox
   echo "<tr>
   <td>$department</td>
   <td>$name</td>
   <td>$email</td>
   <td>$message</td>
   <td><input type='text' name='notes' id='notes'  value='$notes' />
   <td><input type='checkbox' name='checkbox[]' id='checkbox[]'  value=$id />
   <td><input type='hidden' name='id' value=$id />
   </tr>";
    }
   // when the loop is complete, close off the list.
   echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items' style='float:left'/>
   </table><p><input id='update' type='submit' class='button' name='update' value='Update' style='float:left'/></p>
   </form>
   <form action='showContactUs.php' >
    <input type='submit' value='Refresh Records' style='float:left'>
</form>";
   }
?>
PHP代码

<?php
if(isset($_POST['update'])) // from button name="update"

    $hostname = 'xx';
    $username = 'xx';
    $password = 'xx';
    $dbname = 'xx';
    /*** create a new mysqli object with default database***/
    $mysqli = @new mysqli($hostname, $username, $password, $dbname);
    /* check connection */ 
if(!mysqli_connect_errno())
    {
    /*** if we are successful ***/
    echo 'Connected Successfully<br />';
    /*** sql to UPDATE an existing record ***/
    $notes = $_POST['notes'];
     $sql = "UPDATE webquestion 
                SET notes = '$notes'
                WHERE id = '$id'";
    /*** execute the query ***/
    if($mysqli->query($sql) === TRUE)
        {
        echo mysqli_affected_rows($mysqli). ' Records UPDATED successfully<br />';
        }
    else
        {
        echo 'Unable to UPDATE Records: '.$sql.'<br />' . $mysqli->error;
        }
    /*** close connection ***/
    $mysqli->close();
    }
else
    {
    /*** if we are unable to connect ***/
    echo 'Unable to connect';
    exit();
    }
 ?>

谢谢你的帮助。

要通过POST传递数组,只需在name属性中添加一个'[]':

<td><input type='text' name='notes[]' id='notes'  value='$notes' />
<td><input type='hidden' name='id[]' value=$id />

那么在服务器端你可以这样做:

$notes = $_POST['notes'];  //notes array
foreach($_POST['id'] as $index=>$id)  //traverse the ids array
{  
    $note = $notes[$index];  //Get the note on the same row as id
    /*** sql to UPDATE an existing record ***/
     $sql = "UPDATE webquestion SET notes = '$note' WHERE id = '$id'";
    /*** execute the query ***/
    if($mysqli->query($sql) === TRUE)
    {
        echo mysqli_affected_rows($mysqli). ' Records UPDATED successfully<br />';
    }
    else
    {
        echo 'Unable to UPDATE Records: '.$sql.'<br />' . $mysqli->error;
    }
}

如果您想通过匹配它们的id来同时更新几行,您需要一个id数组:

array(25,33,26,24) 

或任何其他类型的数组

然后你应该遍历数组并相应地更新db:

for($i=0; $i < count($id_array); $i++)
{
$id = $id_array[$i];  
$sql = "UPDATE webquestion 
                SET notes = '$notes'
                WHERE id = '$id'";
// and the rest of SQL update

}