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条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧