dpv50040 2013-01-18 05:28
浏览 34
已采纳

$ query-> execute()似乎工作,使用`UPDATE`在MySQL中没有更新值

I'm having a slight issue with prepared statements for PHP and MySQLi. My code takes POST data, prepares a statement and then binds the POST data to the query. After this it executes, seemingly without error, but the statement clearly doesn't ever get executed correctly since no database changes occur.

A quick overview of my problem:

  1. I'm using MySQLi and prepared statements.
  2. I've updated some previous PHP/MySQLi code that was not using prepared statements.
  3. Since updating, a problem has occured with $query->execute().

Code:

if($query = $this->mysqli->prepare("UPDATE article SET copy = ?, title = ?, alias = ?, description = ?, category = ?, category_id = ?, author = ?, permission = ?, mode = ?, comments = ?, revised = ?, frequency = ?, priority = ? WHERE id = ?"))
{
    if
    (
        $query->bind_param
        (
            "sssssdsssssssd", 
            $_POST['edit_article'], 
            $_POST['edit_title'], 
            $_POST['edit_alias'], 
            $_POST['edit_description'], 
            $_POST['edit_category'],
            $result['id'],
            $_POST['edit_author'], 
            $_POST['edit_permission'], 
            $_POST['edit_mode'], 
            $_POST['edit_comments'], 
            $date_time, 
            $_POST['edit_frequency'], 
            $_POST['edit_priority'], 
            $id
        )
    )
    {                   
        if($query->execute())
        {
            echo $this->mysqli->error; // does nothing.

            // These statements print to the screen successfully with the correct values.
            echo $_POST['edit_frequency'] . "<br />";
            echo $_POST['edit_priority'] . "<br />";
            //$_SESSION['article_edited'] = true;
            // die;
            //exit(header("Location: " . __MANAGER__ . $_POST['edit_alias']));
        }
        else
        {
            $_SESSION['article_edited'] = false;

            exit(header("Location: " . __MANAGER__ . $_POST['edit_alias']));
        }
    }
}
else
{
    $_SESSION['article_edited'] = false;

    exit(header("Location: " . __MANAGER__ . $_POST['edit_alias']));
}

Could anybody perhaps explain a better way to debug this code? It makes no sense to me since I check whether the execute() function works with if($query->execute()) and it's successful. Assuming of course the problem does not lie with the execute function, but elsewhere. The problem can't be the POST data since it validates fine with:

if
(
    $_POST['foo'] && isset($_POST['foo']) &&
    $_POST['bar'] && isset($_POST['bar'])
)
{
    // Statements.
}

I am clueless.

EDIT

I would like to note I echo the values of 2 particluar POST vars since those were the ones I added in when updating the code. It has since stopped working. Also note I have used print_r($_POST) and all POST vars are set and contain values.

  • 写回答

2条回答 默认 最新

  • dtnrsmi824877 2013-01-18 07:31
    关注

    Problem has been solved.

    The error was, as expected, not caused by a malfunction or faulty logic, rather an unchanged variable $id, that should have been converted to $_POST['id']. For those that read this in the future, it may be worthwhile noting that Amit Agrawal's reply is actually an important one.

    Please bear in mind that the SQL statement was not in error, it just so happened that no rows were affected since the variable $id was not set or initialised. Therefore, the appropriate course of action would be something along the lines of:

    if($query->execute())
    {
        if($this->mysqli->affected_rows == 0)
        {
            // Appropriate action (such as raising an error), e.g.,
            die("No rows were affected");
        }
        else
        {
            // Appropriate action.
        }
    }
    else
    {
        die("Query could not be executed.");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗