dragon0023 2015-09-09 06:29
浏览 81

使用单选按钮更新记录

I am trying to update some records using radio buttons I want only the selected record to be updated but it keeps on updating the records from beginning to end . Can anyone tell what is that I am missing

 <?php
  $query = mysql_connect("localhost","root","toor");
   mysql_select_db("busticket",$query);
   $result=mysql_query("Select * from ticket_reservation") or      die(mysql_error());
     while($row=mysql_fetch_array($result))
    {
    echo "<tr><td><input type='radio' name='name[]'   value='".$row['id']."'</td><td> '".$row['id']."'</td><td>".$row['userid']."</td> <td>".$row['busid']."</td><td>".$row['numberofseats']."</td></tr>";
     }
     echo "<tr><td><input type='submit' name='submit[]' value='validate'> </td></tr>";
    ?>
   <?php 
    $name=$_POST['name'];

    $qry="UPDATE ticket_reservation set validate_status='Yes'";
    mysql_query($qry);     

   ?>
  • 写回答

1条回答 默认 最新

  • dongzhang6677 2015-09-09 06:47
    关注

    There were a couple of issues with the above code, the main one being that there was no where clause specified to your update statement - thus all records get updated when the form is submitted. The radio button was not closed correctly so would have caused issues with the flow of the html. The update clause requires a POSTed variable name yet there was nothing to prevent the server trying to execute the statement in a normal GET request - hence enclosing in the IF statement.

    /* Create db connection */
    $query = mysql_connect( "localhost", "root", "toor" );
    mysql_select_db( "busticket", $query );
    
    /* Update records */
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        $name=$_POST['name'];
        /* Because the field `name` is called `name[]` - array - you need to specify the item in the array, should be the first one ie: index 0 */
        $qry="UPDATE `ticket_reservation` set `validate_status`='Yes' where `id`='".$name[0]."';";
        mysql_query( $qry );
    }
    
    /* Display records */
    $result=mysql_query("Select * from `ticket_reservation`") or die( 'Error: There was a problem with the query' );
    
    echo "<table>";
    while( $row=mysql_fetch_array( $result ) ) {
        echo "
        <tr>
            <td><input type='radio' name='name[]' value='".$row['id']."'></td>
            <td>".$row['id']."</td>
            <td>".$row['userid']."</td>
            <td>".$row['busid']."</td>
            <td>".$row['numberofseats']."</td>
        </tr>";
    }
    echo "<tr><td><input type='submit' name='submit' value='validate'></td></tr>";
    echo "</table>";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集