[英]MySQLi prepared statement executes twice on insert
I usually don't post detailed versions of my code; 我通常不发布代码的详细版本; however, in this case it may be necessary to figure out the problem.
但是,在这种情况下,可能有必要找出问题所在。 I have a class method that I cannot stop from executing twice.
我有一个无法停止执行两次的类方法。 Is there any particular information I am missing on MySQLi prepared statements?
我在MySQLi预备语句上缺少任何特定信息吗? I have read similar questions that asks the same to no avail.
我读过类似的问题,但都问同样无济于事。
I previously asked a question about using eval for dynamic queries in prepared statements as far as it's convention and best practices. 之前,我曾问过一个问题,即按照惯例和最佳做法,如何在准备好的语句中使用eval进行动态查询。 I was told to use call_user_func_array() and it worked perfectly;
有人告诉我使用call_user_func_array(),它工作得很好。 however, I failed to notice that the statement executed twice each time, even with the old eval() code.
但是,我没有注意到该语句每次执行两次,即使使用旧的eval()代码也是如此。 So I put together a snippet of my ACTUAL code which should pretty much explain itself through my comments
因此,我整理了一段ACTUAL代码,该代码应该可以通过我的注释进行自我解释
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>");
}
}
Changed code format from OOP to regular function for testing without having to create a class. 将代码格式从OOP更改为常规功能以进行测试,而无需创建类。
old question, but people might still stumble upon it, I did. 这是个老问题,但人们还是会偶然发现,我做到了。
I think the mysqli_query function is mainly for retrieving data, I had the same problem, and fixed it by using mysqli_real_query on insert and update queries. 我认为mysqli_query函数主要用于检索数据,我遇到了同样的问题,并通过在插入和更新查询中使用mysqli_real_query进行了修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.