在表单提交上更新数据库字段 - PHP

时间:2013-01-01 10:01:30

标签: php mysql sql database

这样做是从db获取数据,其中'approve'字段的值为'0'。显示表单。现在我们要做的是在点击“Approve”按钮时将'approve'字段的值更新为'1'。我认为IF条件存在一些问题,不确定。连接到数据库没有问题。或者我是否需要关闭数据库连接或提交或更新以进行更新,不确定。谢谢你的帮助。

require("dbconn.php");
// get the form data and store it in the database
// show database data
$query="SELECT * FROM page where approve=0";
$result=mysql_query($query);

if ($result)
{
    print "<b>Approval pending for below listings.</b><br><br>";
    while($row = mysql_fetch_array($result))
    {
        echo '<form name="submit_form" action="" method="post">';

        $page_url = $row['page_url'];
        $contact_number = $row['contact_number'];
        $description = $row['description'];
        $category = $row['category'];
        $address = $row['address'];
        $business_name = $row['business_name'];

        echo "<input type=\"text\" name=\"business_link\" value=\"$page_url\" readonly><br/>";
        echo "<input type=\"text\" name=\"contact_number\" value=\"$contact_number\" readonly><br/>";
        echo "<input type=\"text\" name=\"description\" value=\"$description\" readonly><br/>";
        echo "enter code here`<input type=\"text\" name=\"category\" value=\"$category\" readonly><br/>";
        echo "<input type=\"text\" name=\"address\" value=\"$address\" readonly><br/>";
        echo "<input type=\"text\" name=\"business_name\" value=\"$business_name\" readonly><br/>";
        echo "<input type=\"Submit\" Value=\"Approve\" name=\"submit\"/>";
        echo "</form>";
        echo "<hr><br>";
        if($_POST['submit_form'] == "submit")
        {
                mysql_query("UPDATE page SET approve='1' WHERE business_name='$business_name' AND contact_number='$contact_number' AND page_url='$page_url' AND description='$description' AND address='$address' AND category='$category'");
                echo "Thank you!";
        }
    }
}
else
{
    print mysql_error();
}

4 个答案:

答案 0 :(得分:1)

您正在检查“submit_form”,但您应该从“提交”按钮检查“提交”。

答案 1 :(得分:1)

变化: -

if($_POST['submit_form'] == "submit")

以下: -

if(isset($_POST['submit']) && $_POST['submit'] == "Approve")

另外,你应该把上面的代码放在“require(”dbconn.php“);”之后,如下所示: -

require("dbconn.php");
if(isset($_POST['submit']) && $_POST['submit'] == "Approve"){
   // update query here
   // show notification
   // you can show form too
}else{
   // display the form
}

答案 2 :(得分:0)

使用

if(isset($_POST['submit_button_name']))

答案 3 :(得分:0)

尝试使用php代码isset($_POST['submit'])并检查按钮是否正在提交。还要删除<input type="Submit"......><input type="submit"......>。回显SQL查询并检查所有值是否都在进入查询。