如何从mysql中选择不同的行并在单个页面中更新它们

时间:2013-03-05 09:29:03

标签: php mysqli multiple-columns

我想从sql数据库中选择一些行。第一个用户应该插入两个我可以选择此行的变量。然后在这个页面中有一个表将更新这行,但问题是当我选择没有输入的列时我能够更新数据库但是当我想使用输入变量时它会显示表但不能更新它。我在哪里弄错了?

在不使用输入的情况下选择数据库列:

<?php
include 'mytool.php';
        // username and password sent from form
    $myusername=$_SESSION["mys"];

    $host="localhost"; // Host name
    $username='username'; // Mysql username
    $password='password'; // Mysql password
    $db_name="db"; // Database name
    $tbl_name="test"; // Table name

    // Connect to server and select database.

     $link=mysql_connect($host,$username,$password)or die("cannot connect");
    $selected=mysql_select_db($db_name,$link)or die("cannot select DB");
    $sql="SELECT * FROM $tbl_name where classnum='106' and level='i4' ";

    $result=mysql_query($sql,$link);

    $count=mysql_num_rows($result);     

?>

输入变量:

<form id="searchform" method="post" dir="rtl" action=""> 
                    level :<input name="level"  type="text" id="level" />
                    classnumber :<input name="classnum"  type="text" id="classnum" />

                           <input style="margin-top:10px; margin-left:120px; font-size: 14px;  padding: 5px 14px;" type="submit" value="search" name="enter"   />

                </form>

                <?php   
                    if(isset($_POST['enter'])){         
                    $sql="SELECT * FROM $tbl_name where classnum='106' and level='i4' ";

                    $result=mysql_query($sql,$link);

                    $count=mysql_num_rows($result);
                    }
                ?>

在上面的代码后更新表格代码:

 <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']; ?>" >
                                                    </td>
                                                    <td align="center"><input type="text" name="name[]" id="name" value= "<? echo $rows['name']; ?>"  /></td>
                                                    <td align="center"><input type="text" name="lastname[]" id="lastname" value= "<? echo $rows['lastname']; ?>"  /></td> 
                                                    <td align="center"><input type="text" name="midmark[]" id="midmark" /></td>
                                                    <td align="center"><input type="text" name="classmark[]" id="classmark" /></td>
                                                    <td align="center"><input type="text" name="finalmark[]" id="finalmark" /></td>
                                                    <td align="center"><input type="text" name="state[]" id="state" /></td>
                                                    <td align="center"><input type="text" name="details[]" id="details"  /></td>

                                                </tr>
                                                <?php
                                                 }
                                                 ?>
                                            </table>
                                        </div>

                                <input 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();
                                ?>

1 个答案:

答案 0 :(得分:1)

比较这些线条。

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);**
                 }
           }

与这一个。

$sql1=" 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);