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

Php 使用WHILE循环更新回显数据。只更新一条记录,php,mysql,forms,post,while-loop,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.** m

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

<?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'"
        );
    } 

    ?>

每次提交表单时只更新第一条记录,因为您已设置了
$id=$\u POST['m\u id'][0]
,其中包含第一条
类型[]
文本框的值。要同时更新所有其他记录,请循环执行
$\u POST['m\u id']

替换它。希望这能奏效

<?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++;
        }
    } 

    ?>
试试这个:

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]'"
          );
       }
} 
if(isset($_POST[“action”])=“update”)
{
$id=$\u POST['m\u id'];
$type=$_POST['type'];
$loopcount=计数($id);

对于($i=0;$i您的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>


第一:不要使用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 } ?>
'method='post'>

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

不要使用mysql\u query,它已被弃用。您的代码也可以进行sql注入。让我试试最后一个:)它将同时更新所有这些按钮。我想这很好。我不需要所有的更新按钮。我想只有一个
<?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));

} 
<?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 } ?>