0

I got a table with dynamic data with 5 td-s. First one is for the ID, second one for date, third for the name of the author, fourth for some properties and in the last one i got two buttons. I want them to change the value of the $status in applications table. For that I made 2 php files in which I added the mysql update function for each of the buttons. But I don't know why when I press the buttons it does everything in the php except it doesn't change the value of $status. Please let me know where I am wrong and how can I make it work. Thanks in advance.

The html code of the buttons (the last td):

<form action="status1.php">
<input type="submit" name="approve" value=" + ">
</form>
<form action="status2.php">
<input type="submit" name="refuse" value=" - ">
</form>

The PHP code for the buttons - status1.php (status2.php is the same but it changes the $status value to 2 instead of 1)

<?php
    require_once('config.php');
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_query('set names windows-1251', $link);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }
    $id=$_GET['id'];
    $qry="UPDATE applications SET status=1 WHERE id='$id'";
    $result = mysql_query($qry);
        if($result) {
            header("location: applications.php");
            exit();
        }
        else {
            die("Query failed");
        }
?>
4

3 回答 3

2

您正在使用$_GET['id']作为标识符,但据我在代码中看到的,除了提交按钮本身之外,您实际上并没有发送任何 GET 信息。因此,您的查询当前实际上正在更新 row WHERE id=''。这就是为什么你没有得到错误,但你也没有得到你想要的结果。

将表单的 action 参数更改为status1.php?id=$id,或在表单内添加类似<input type="hidden" name="id" value="$id"/>的内容。

于 2012-09-14T15:52:10.627 回答
1

嗯,你有什么错误吗?注释掉标题(“位置:applications.php”);线,所以你会看到它是否抛出任何东西。还可以尝试添加类似 echo $qry 的内容,这样您就可以直观地验证查询是否正确。

此外,您应该阅读 SQL 注入以及如何防范它。像这样直接将用户输入粘贴到查询中可能会带来麻烦。此外,您没有检查用户输入的撇号,这可能会破坏您的查询。我个人使用 PDO,这使它更容易也更安全。

另一个建议,不必维护两个单独的提交 PHP 文件,只需像这样放置两个提交按钮:

<input type="submit" name="status" value=" + ">
<input type="submit" name="status" value=" - ">

然后将表单操作更改为合并的 php 文件的名称,在该文件中,只需评估状态的值,如:

$status = 0;
if ($_GET["status" == " + ") $status = 1;

如果你安装 PDO,你会像这样进行数据库更新:

$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE, DB_USER, DB_PASSWORD);
$sql = $pdo->prepare("UPDATE applications SET status=? WHERE id=?");
$sql->execute(array($status, $_GET["id"]));

..这会比你现在做的更安全。

免责声明:我只是一个业余爱好者 PHP 程序员,所以可能有比我提到的更好的方法 :)

于 2012-09-14T16:03:06.930 回答
0

使用这个而不是你的表单标签作为表单 1

<from method="get" action="status1.php">

<input type="hidden" name="id" value="1"/>

<input type="submit" name="approve" value=" + "/>

</form>

表格 2

<from method="get" action="status2.php">

<input type="hidden" name="id" value="2"/>

<input type="submit" name="refuse" value=" - "/>

</form>
于 2012-09-14T15:55:27.830 回答