在提交表单上使用PHP

时间:2017-02-02 10:29:03

标签: php html mysql

我有一个表格,显示有回声,其中,对于每一行,我想要一个按钮来更改“状态”字段(我想只更新一个字段)。我试图在我的回声中插入一个表单按钮,但我什么也得不到。

这是我的回音代码:

while($row = mysql_fetch_array( $result )) {
    echo "<tr>";
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['fullname'] . '</td>';
        echo '<td>' . $row['message'] . '</td>';
        echo '<td>' . $row['country'] . '</td>';
        echo '<td>' . $row['email'] . '</td>';
        echo '<td>' . $row['website'] . '</td>';
        echo '<td>' . $row['date'] . '</td>';
        echo "<td>" . $row['status'] . " </td>";

        echo "<form action='updatestatus.php' method='post'>";
            echo "<input type='hidden' value='".$row['id']."' name='id' />";
            echo "<input type='hidden' value='".$row['status']."' name='status' />";
            echo "<td><input type='submit' name='submit' value='Submit'></td>";
        echo "</form>";

        echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
        echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
    echo "</tr>"; 
}
  

updatestatus.php

// check if the form has been submitted. 
if (isset($_POST['submit'])) { 
    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['id'])) {
        // get form data, making sure it is valid
        $id = $_POST['id'];
        $status = $_POST['status'];

        if ($status == 0) {
            $status == 1;
            mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
            or die(mysql_error());
        } else {
            $status == 0;
            mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
            or die(mysql_error()); 
        }
        header("Location: view.php"); 
    }
}

所以,如果我的状态字段是= 0,我想改变它= 1并更新到我的数据库,反之亦然。

感谢您的关注

2 个答案:

答案 0 :(得分:1)

您的表单没有向updatestatus.php发送任何内容,您可以以隐藏的形式将数据添加到POST中:

echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";

这是对上述代码的简单修复,其他人在评论中提到了一些安全漏洞和效率方法。

答案 1 :(得分:0)

检查此PHP代码

while($row = mysql_fetch_array( $result )) {

    echo "<tr>";
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['fullname'] . '</td>';
    echo '<td>' . $row['message'] . '</td>';
    echo '<td>' . $row['country'] . '</td>';
    echo '<td>' . $row['email'] . '</td>';
    echo '<td>' . $row['website'] . '</td>';
    echo '<td>' . $row['date'] . '</td>';
    echo "<td>" . $row['status'] . " </td>";
    echo "<td>";
    echo "<form action='updatestatus.php' method='post'>";
    echo "<input type='hidden' value='".$row['id']."' name='id' />";
    echo "<input type='hidden' value='".$row['status']."' name='status' />";
    echo "<td><input type='submit' name='submit' value='Submit'></td>";
    echo "</form>";
    echo "</td>";
    echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
    echo "</tr>"; 

}
  

updatestatus.php

 // check if the form has been submitted. 
     if (isset($_POST['submit']))
     { 
     // confirm that the 'id' value is a valid integer before getting the form data
     if (is_numeric($_POST['id']))
     {
     // get form data, making sure it is valid
     $id = $_POST['id'];

     $status = $_POST['status'];

     if ($status == 0){
         $status == 1;
         mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='".$id."'")
     or die(mysql_error());
     } else {
          $status == 0;
         mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='$id'")
     or die(mysql_error());

     }
       header("Location: view.php"); 
     }
  }