仅更新上一个数据库记录 - 需要数组吗?

时间:2014-03-09 12:32:25

标签: php html sql arrays

有人可以帮助我,我只是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();
    }

 ?>

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

如果要通过匹配ID来同时更新多个行,则需要一组ID:

array(25,33,26,24) 

或任何其他类型的数组。

然后你应该遍历你的数组并相应地更新数据库:

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

}

答案 1 :(得分:0)

要通过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;
    }

}