在php中更新过滤的mysql行

时间:2013-03-04 19:55:20

标签: php mysql filtered-lookup

我正在尝试更新mysql中不同的行,这些行是按照某些记录过滤的。在此代码中,用户首先应输入两个输入:classnumber和level。 他们拥有此字段的记录将显示在表中,然后用户应该能够更新此记录信息。但问题是所有的记录都在mysql的一个表中,所以他们的id不同我的意思是例如第一个匹配两个输入id的记录是2然后下一个是5,下一个是6和.... / p>

在这种情况下我应该如何更改下面的代码?

<form id="searchform" method="post" dir="rtl" action=""> 
 level :<input name="level"  style="margin-top:5px; margin-left:6px;font-family: Tahoma,Geneva,sans-serif; font-size: 12px; margin-top: 5px; padding: 5px; width: 50px;" type="text" id="level" />
 classnumber :<input name="classnum"  style="margin-top:5px; margin-left:6px;font-family: Tahoma,Geneva,sans-serif; font-size: 12px; margin-top: 5px; padding: 5px; width: 50px;" type="text" id="classnum" />
 <input style="margin-top:10px; margin-left:120px; font-size: 14px;  padding: 5px 14px;" type="submit" value="جستجو" name="enter"   />
</form>

<?php
 if(isset($_POST['enter'])){
  $sql="SELECT * FROM  `".$tbl_name."` where classnum='".$_REQUEST['classnum']."' and level='".$_REQUEST['level']."' ";
  $result=mysql_query($sql,$link);
  $count=mysql_num_rows($result);
 }
?>

<div class="cleaner h30"></div>
<br>

 <form name="form1" action="" method="POST">
  <center>
   <div>                                
    <div align="center" width = 615>
    <table class="stats" cellspacing="0" width="615" border="1">
    <tr>
      <th width="20" scope="col" >Id</th>
     <th width="60" scope="col">Name</th>
     <th width="60" scope="col">Last Name</th>
     <th width="42" scope="col">Midterm</th>    
     <th width="54" scope="col">Class mark</th>
     <th width="42" scope="col">Final Quiz</th>
     <th width="54" scope="col">State</th>
     <th width="54" scope="col">Details</th>
    </tr>
                                            <?php
      while($rows=mysql_fetch_array($result)){
      $id[]=$rows['id'];
    ?>
                                        <tr>
      <td align="center">
     <input name="id[]" type="text" id="id" value="<? echo $rows['id']; ?>"  style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 10px;"></td>
      <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 60px;" type="text" name="name[]" id="name" value= "<? echo $rows['name']; ?>"  /></td>
     <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 60px;" type="text" name="lastname[]" id="lastname" value= "<? echo $rows['lastname']; ?>"  /></td> 
     <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 50px;" type="text" name="midmark[]" id="midmark" /></td>
     <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 50px;" type="text" name="classmark[]" id="classmark" /></td>
     <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 50px;" type="text" name="finalmark[]" id="finalmark" /></td>
     <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 50px;" type="text" name="state[]" id="state" /></td>
     <td align="center"><input style=" font-family: Tahoma,Geneva,sans-serif; font-size: 12px;  padding: 5px; width: 50px;" type="text" name="details[]" id="details"  /></td>
    </tr>

     <?php
                                         }
                                         ?>
                                            </table>
                                        </div>
    </div>
   </center>
   <input style="margin-top:10px; float:right; margin-right:175px; font-size: 14px;  padding: 5px 14px;" type="submit" value="ثبت" name="Submit"   />
  </form>

<?php
 // Check if button name "Submit" is active, do this 
 if(isset($_POST['Submit']))
  {
   for($i=0;$i<$count;$i++)
    {
    $sql1=mysql_query(" UPDATE `".$tbl_name."` SET midmark='".$_REQUEST['midmark'][$i]."' , classmark='".$_REQUEST['classmark'][$i]."' , finalmark='".$_REQUEST['finalmark'][$i]."' , state='".$_REQUEST['state'][$i]."' , details='".$_REQUEST['details'][$i]."'  WHERE id='".$_REQUEST['id'][$i]."' ");                           $result1=mysql_query($sql1);
    }
   }

if($result1){
 header("location:results.php");
  }
  mysql_close();
?>

0 个答案:

没有答案