【问题标题】:MySQLi prepared statement executes twice on insert [duplicate]MySQLi准备好的语句在插入时执行两次[重复]
【发布时间】:2016-01-15 05:00:37
【问题描述】:

我通常不会发布我的代码的详细版本;但是,在这种情况下,可能需要找出问题所在。我有一个无法停止执行两次的类方法。 MySQLi 准备好的语句中是否缺少任何特定信息?我已经阅读了类似的问题,但都无济于事。

我之前问过一个关于在准备好的语句中使用 eval 进行动态查询的问题,就其约定和最佳实践而言。有人告诉我使用 call_user_func_array() 并且效果很好;但是,我没有注意到该语句每次执行两次,即使使用旧的 eval() 代码也是如此。所以我把我的实际代码的 sn-p 放在一起,这应该可以通过我的 cmets 来解释自己

    function insert($table, $query)
    {
        /**
         *
         * This code assumes you have a MySQLi connection stored in variable $db
         * USAGE: insert(table, array('field' => 'value');
         * 
        **/

        // Sets the beginning of the strings for the prepared statements

        $fields = $values = "(";
        $types = "";
        $params = array();

        foreach($query as $key => $val)
        {               
            // array keys = fields, and array values = values;
            $fields.= $key;

            // concatenate the question marks for statement
            $values.= "?";

            // concatenate the type chars
            $types.= is_string($val) ? "s" : (is_int($val) ? "i" : (is_double($val) ? "d" : "b"));

            // pass variables to array params by reference for call_user_func_array();
            $params[] = &$query[$key];

            if($val == end($query))
            {
                $fields .= ")";
                $values .= ")";
                array_unshift($params, $types);
            }
            else
            {
                $fields .= ", ";
                $values .= ", ";
            }
        }
        $str = "INSERT INTO {$table} {$fields} VALUES {$values}";
        if($stmt = $db->prepare($str))
        {
            call_user_func_array(array($stmt, 'bind_param'), $params);

            /**
             *
             * This is where I am pulling my hair out of my head and being 3
             * nothces away from banging my own head into the screen and
             * being without a computer at all.
             *
             * I have tried everything I can think of. I gotta be missing
             * something
             *
             * IT JUST KEEPS SENDING 2 ROWS DANG IT!
             *
             **/


            /////////////////////
            $stmt->execute();//// <---Help is needed here
            /////////////////////

            //-- Close connection;
            $stmt->close();
        }
        else
        {
            //-- Send a nice readable error msg
            die("<center><h3>FAULTY QUERY STRING</h3><h4>Please check query string</h4><p>{$str}</p>");
        }
    }

将代码格式从 OOP 更改为常规函数以进行测试,而无需创建类。

【问题讨论】:

  • 这个问题看起来和this one很像,但实际上从未得到回答。
  • 你确定你没有给insert()打两次电话吗?这段代码对我来说是正确的。
  • 检查SQL->准备它可能被调用有可能吗?
  • 我不确定。我可能不得不把它分解成函数,看看它是否独立完成。它可能在我的 __construct()
  • @TimBiegeleisen 实际上是这样。我创建了一个新页面,将 $db 测试为 MySQLi,并使用类方法作为上述函数,结果相同。我的服务器上可能存在配置错误吗?

标签: php mysqli prepared-statement sql-insert dynamicquery


【解决方案1】:

老问题,但人们可能仍然会偶然发现它,我做到了。

我认为 mysqli_query 函数主要用于检索数据,我遇到了同样的问题,并通过在 insert 和 update 查询上使用 mysqli_real_query 来修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2016-04-05
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2014-03-29
    相关资源
    最近更新 更多