使用WHILE循环更新回显数据。仅更新一条记录

时间:2013-04-20 02:55:38

标签: php mysql forms post while-loop

我似乎无法更新除第一个以外的任何记录。 我不知道如何修改任何显示的记录。

<?php

    if(isset($_POST["action"]) == "update")
    {
        $id = $_POST['m_id'][0];
        $type = $_POST['type'][0];
        // if I echo $id & $type, it only gives me the first record.**
        mysql_query("
          UPDATE membership_type
          SET mt_type ='$type'
          WHERE mt_id = '$id'"
        );
    } 

    ?>

这是在同一个php页面中。

<form name=form action='' method='post'>

  <?php 
  $result=mysql_query("SELECT * FROM membership_type;");
  while($rows=mysql_fetch_array($result))
  {  ?>
    <input size=35 class=textField type=text name='type[]' value='<?php echo  $rows['mt_type']; ?>'>
    <input type=hidden name='m_id[]' value="<?php echo $rows['mt_id']; ?>">
    <input type=submit value="Update">
  <?php 
  } 
  ?>

如何通过单击“更新”按钮???

来编辑任何显示的记录

5 个答案:

答案 0 :(得分:0)

每个表单提交都只更新第一条记录,因为您已设置$id = $_POST['m_id'][0],其中包含第一个type[]文本框的值。要更新所有其他记录,请循环浏览$_POST['m_id']

答案 1 :(得分:0)

替换它。希望这有效。

<?php

    if(isset($_POST["action"]) == "update")
    {
        $id = $_POST['m_id'];
        $type = $_POST['type'];
        $i = 0;
        foreach($id as $mid) {
             mysql_query("UPDATE membership_type
                         SET mt_type='".mysql_real_escape_string($type[$i])."'
                         WHERE mt_id = '".intval($mid)."'") OR mysql_error();
        $i++;
        }
    } 

    ?>

答案 2 :(得分:0)

试试这个:

if(isset($_POST["action"]) == "update")
{
        $id = $_POST['m_id'];
        $type = $_POST['type'];
        $loopcount = count($id);
        for($i=0; $i<$loopcount; $i++)
        {
          mysql_query("
           UPDATE membership_type
           SET mt_type ='$type[$i]'
           WHERE mt_id = '$id[$i]'"
          );
       }
} 

答案 3 :(得分:0)

您的HTML格式错误,您作为数组传递,但之后只使用第一个元素。考虑:

<form name="form" action="" method="post">

<?php 
$result = mysql_query("SELECT * FROM membership_type;");
while($row = mysql_fetch_array($result))
    echo sprintf('<input size="35" class="textField" type="text" name="m_ids[%s]" value="%s" />', $row['mt_id'], $row['mt_type']);
?>
    <input type="submit" value="Update">
</form>

然后是服务器脚本:

<?php

if(isset($_POST["action"]) && $_POST["action"] == "Update"){

    foreach($_POST['m_ids'] as $mt_id => $mt_type)
        mysql_query(sprintf("UPDATE membership_type SET mt_type ='%s' WHERE mt_id = %s LIMIT 1", addslashes($mt_type), (int) $mt_id));

} 

你可以在这里做其他事情,例如。准备好的陈述,但这应该有用。

答案 4 :(得分:0)

首先:你应该从不使用 mysql _ *函数,因为它们已被弃用。
第二:试试这段代码:

<?php  

// Get a connection to the database
$mysqli = new mysqli('host', 'user', 'password', 'database');

// Check if there's POST request in this file
if($_POST){ 

    foreach($_POST['m_id'] as $id => $type){
      $query = "UPDATE membership_type
                SET mt_type = '".$type."'
                WHERE mt_id = '".$id."'";

      // Try to exec the query
      $mysqli->query($query) or die($mysqli->error);
    }
}else{ 

  // Get all membership_type records and then iterate
  $result = $mysqli->query("SELECT * FROM membership_type") or die($mysqli->error); ?>

  <form name='form' action='<?php echo $_SERVER['PHP_SELF'] ?>' method='post'>
    <?php while($row = $result->fetch_object()){ ?>
      <input size='35' 
             class='textField' 
             type='text' 
             name='m_id[<?php echo $row->mt_id ?>]' 
             value='<?php echo $row->mt_type; ?>'>
      <input type='submit' value="Update">
    <?php } ?>
  </form>

<?php } ?>

第三:为了增加安全性(此代码易受攻击),请尝试mysqli_prepare