使用 while with 下拉列表更新 MySQL 数据库上的活动数据


use while with drop down list to update active data on mysql database

(i have drop down list with two options with values),    
> >   <select  name="status"  id="statue"><option value="0"><option 
      value="1"></select>i use select tag    
> > 
> >    
  • 与 while 如下面的代码所示,这两个选项 考虑

    active for status, 0=not active,1=active , and i have two column     
        name 
      active and column name email on mysql database , my problem when 
        select 
      option value =1, the active column on mysql database update to
      1 for all rows on active column , and i want to update active for
      selected value that i choose and not for all rows on active column 
    <?php 
       $qur=mysql_query("select * from sale");
       while($row=mysql_fetch_array($qur))  
       /*  here using loop to Retrieval data from database with select
           to   
               make active 
       */ and change value from 0 to 1 for only selected value and not for all
    
       {
       ?>
       <tr>
       <td><select name="status[]" id="status" ><option
       selected>.....</option><option value="0">not active</option><option
       value="1">active</option></select></td>
       <td align="center"><?php echo $row['active'];?></td>
       <td><?php echo $row['email'];?></td>
       <td><?php echo $row['id'];?></td>
       </tr>
       <?php } ?>
       </table>
       <input type="submit" name="chk" value="Get Selected Values" />
       <?php
         if(isset($_POST['chk']))
          {
            foreach ($_POST['status'] as $select)
           {
              echo "You have selected :" .$select; // Displaying Selected  
               Value
              $sqlr=mysql_query("UPDATE sale SET active='$select'  WHERE 
              id='".$row['id']."';");
           }
          } 
       ?>
    

    当按提交然后返回我想要的所有值 0 或 1 时 按提交,然后仅激活所选项目并更新 数据库上的值

你必须把

WHERE 子句放在你的查询中,这是一个 SQL 语句问题,你必须告诉 SQL 你要修改哪一行,否则它会修改所有行。试试这个:

 $sqlr=mysql_query("UPDATE sale SET active='".$select."' WHERE id = ".$row['id']." ;");

如果需要,请更改"id"以匹配表中的主键字段,假设$row['id'] 是该行的 PK 值,$select是在"活动"字段中设置的值。

编辑:如果您使用 active 作为 VARCHAR 变量,则需要放置单引号,否则不要使用单引号。您确定字段 ID 是 PK 并且变量 $row['id'] 填充了您要更改的实际行 ID 吗?

你不引用任何数据集。

$sqlr=mysql_query("UPDATE sale SET active='$select' WHERE [...]");