sql查询在某些时候有效,而在其他时候则没有

时间:2012-03-15 16:08:05

标签: php mysql

我在管理面板中设置了启用/禁用按钮

http://brettadamsga.com/development/madisonweddings/admin/clients.php

按钮有时会起作用,然后在其他时候不起作用,我不知道是什么原因引起的。有什么想法吗?

创建客户列表

<?php
    dbcon();
    $navQuery = 'SELECT * FROM mw_clients WHERE status = 1';
    $li = mysql_query($navQuery);
    while($row = mysql_fetch_array($li){
        echo "<tr><td><a href=\"index.php?loc=" . $row['clientSlug'] . "\">" . $row['name'] . "</a></td><td align=\"center\"><a style=\"text-decoration: none;\" href=\"changeStatus.php?type=2&id=" . $row['id'] . "&status=0\">&#10005;</a></td></tr>";
    }
?>

changeStatus.php

<?php
require ('../common.php');

$type=$_GET["type"];
$id=$_GET["id"];
$status=$_GET["status"];

if ($status == 0 && $type == 1)
    {
        dbcon();
        $query = "UPDATE mw_cats SET status=0 WHERE id=" . $id . "";
        if (mysql_query($query))
            {
                echo mysql_error();
                mysql_close();
                header ('HTTP/1.1 301 Moved Permanently');
                header ('Location: categories.php');
            } else {
                echo '<p>Could not change status</p>';
            }
    }

if ($status == 1 && $type == 1)
    {
        dbcon();
        $query = "UPDATE mw_cats SET status=1 WHERE id=" . $id . "";
        if (mysql_query($query))
            {
                echo mysql_error();
                mysql_close();
                header ('HTTP/1.1 301 Moved Permanently');
                header ('Location: categories.php');
            } else {
                echo '<p>Could not change status</p>';
            }
    }

if ($status == 0 && $type == 2)
    {
        dbcon();
        $query = "UPDATE mw_clients SET status=0 WHERE id=" . $id . "";
        if (mysql_query($query))
            {
                echo mysql_error();
                mysql_close();
                header ('HTTP/1.1 301 Moved Permanently');
                header ('Location: clients.php');
            } else {
                echo '<p>Could not change status</p>';
            }
    }

if ($status == 1 && $type == 2)
    {
        dbcon();
        $query = "UPDATE mw_clients SET status=1 WHERE id=" . $id . "";
        if (mysql_query($query))
            {
                echo mysql_error();
                mysql_close();
                header ('HTTP/1.1 301 Moved Permanently');
                header ('Location: clients.php');
            } else {
                echo '<p>Could not change status</p>';
            }
    }

?>

3 个答案:

答案 0 :(得分:1)

我发现当mysql_error返回true(成功)时回显mysql_query而不是失败则回复{<1}}。

mysql_error返回false时,尝试将mysql_query移至if语句的第二部分。

修改

根据您的意见,这里有更多建议。

在代码中添加一些故障排除声明。如果没有调试器或逐步执行代码的方法,一些echo语句在确定发生的事情时非常有用。例如,在第一个if语句之前,添加:

echo 'Status: ' . $status . '<br />';
echo 'Type: ' . $type . '<br />';

或更好:

var_dump($_GET);

找出$ _GET数组中的内容。

假设有效,请确保您的连接成功:

dbcon() or die(mysql_error());

(您可能必须编辑dbcon()方法,以便在实际尝试连接时捕获错误的机会,您的dbcon()方法必须在失败时返回布尔值false,以便{ {1}}正常工作。)

接下来,确保根据需要构建查询。另一个echo语句可以提供帮助:

or die

此查询每次都是正确的吗?如果是这样,请尝试在您选择的mysql管理器中按原样执行查询(WorkbenchSqlyog等)。这样,您可以确定数据库是生成错误还是执行查询的时间非常长。

如果一切似乎都在工作仍然那么你应该在你的表中进行一些挖掘,看看你正在改变的数据是否真的在改变。也许存在权限问题或其他问题(尽管这应该出现在echo 'Query: ' . $query . '<br />'; 中。)

mysql_error

希望其中一条建议有所帮助!

答案 1 :(得分:1)

您可以将整个代码替换为:

,从而删除所有重复项
require ('../common.php');

$type = $_GET["type"];
$id = $_GET["id"];
$status = $_GET["status"];

dbcon();

$table = "mw_clients";
if ($type == "1") {
    $table = "mw_cats";
}

$query = "UPDATE ".$table." SET status=".mysql_real_escape_string($status)." WHERE id=" . mysql_real_escape_string($id) . "";
if (mysql_query($query))
{
    header ('HTTP/1.1 301 Moved Permanently');
    header ('Location: clients.php');
} else {
    echo '<p>Could not change status '. mysql_error() .'</p>';
}

还添加了mysql_real_escape_string()以确保安全性(任何人都可以在浏览器中调用此脚本并对数据库执行操作)并将$type检查为string而不是number }。

我不知道为什么会失败 - 但现在你有一些实际的错误检查(移动mysql_error())所以它应该更容易调试

答案 2 :(得分:1)

这是由您的永久重定向引起的。它们会被浏览器缓存,因此下次访问时会直接转到重定向页面,而不是运行脚本。