php mysql将我的更新和删除页面引导到主页

时间:2015-12-05 04:22:39

标签: php mysql

这些是代码 您认为我应该在哪里插入标题(位置:' home.php') 我尝试在if语句之后使用,这些是代码

      else {
      try {
      header('Location: home.php');
                            }  

但即使我没有点击更新按钮,也直接转到home.php

        <?php
     if (isset($_POST['update'])){

     $UpdateQuery = "UPDATE registry SET db_id = '$_POST[db_id]', db_name = 
     '$_POST[db_name]', db_age = '$_POST[db_age]', db_gender = 
     '$_POST[db_gender]', db_birthdate = '$_POST[db_birthdate]', db_phone = 
     '$_POST[db_phone]', db_address = '$_POST[db_address]' 
     WHERE db_id = '$_POST[hidden]'";
     mysql_query($UpdateQuery);
     }

          $sql = "SELECT * FROM registry";
          $query = mysql_query($sql) or throw_ex(mysql_error());  ;

        echo"<h2><center>Employee Masterlist</center></h2>";
        echo "<table class = \"form\" border = \"1\" cellspacing = \"3\">";
        echo "<tr>";
        echo "<th>Employee ID</th>";
        echo "<th>Name</th>";
        echo "<th>Age</th>";
        echo "<th>Gender</th>";
        echo "<th>Birthdate </th>";
        echo "<th>Phone No. </th>";
        echo "<th>Address </th>";
        echo "</tr>";



            while($row = mysql_fetch_array($query))
            {
                    echo "<form action=update.php method=post>";
                    echo "<tr>";
                    echo "<td>" . "<input id=input-up type=text name=db_id 
           value=" . $row['db_id'] . "> </td>";
                    echo "<td>" . "<input id=input-up type=text name=db_name
           value=" . $row['db_name'] . "> </td>";
                    echo "<td>" . "<input id=input-up type=text name=db_age 
           value=" . $row['db_age'] . "> </td>";
                    echo "<td>" . "<input id=input-up type=text 
           name=db_gender value=" . $row['db_gender']. "> </td>";
                    echo "<td>" . "<input id=input-up type=text 
           name=db_birthdate value=" . $row['db_birthdate'] . "> </td>";
                    echo "<td>" . "<input id=input-up type=text 
           name=db_phone value=" . $row['db_phone'] . "> </td>";
                    echo "<td>" . "<input id=input-up type=text 
           name=db_address value=" . $row['db_address'] . "> </td>";
                    echo "<input type=hidden name=hidden value=" . 
           $row['db_id'] . "> </td>";
                    echo "<td>" . "<input class=send_btn type=submit 
           name=update value=Update". "> </td>";
                    echo "</tr>";
                    echo "</form>";

            }

            echo"</table>";
            mysql_close();
          ?>    
           </div>
           </body>
           </html>

1 个答案:

答案 0 :(得分:2)

您应该在执行查询后添加它。 这是代码

 if (isset($_POST['update'])){

 $UpdateQuery = "UPDATE registry SET db_id = '$_POST[db_id]', db_name = 
 '$_POST[db_name]', db_age = '$_POST[db_age]', db_gender = 
 '$_POST[db_gender]', db_birthdate = '$_POST[db_birthdate]', db_phone = 
 '$_POST[db_phone]', db_address = '$_POST[db_address]' 
 WHERE db_id = '$_POST[hidden]'";
 mysql_query($UpdateQuery);
 //redirect to home.php
 header('Location: home.php');
 }