[英]PDO multi insert statement
I've got a working insert for a single input field but when I try to add a couple more it seems to break everything. 我为单个输入字段提供了一个有效的插入内容,但是当我尝试添加更多内容时,似乎会破坏所有内容。 I have a database connection working fine and including that correctly at the top of the page when I change the "isset" to have the 3 columns it breaks.
我的数据库连接工作正常,当我将“ isset”更改为中断的三列时,该连接正确显示在页面顶部。
This is my set statement; 这是我的既定声明;
if(isset($_POST['title, question, tags']))
{
$success = insertData('questions', 'title', $_POST['title']);
$success = insertData('questions', 'question', $_POST['question']);
$success = insertData('questions', 'tags', $_POST['tags']);
if(!$success)
echo 'Sorry failed :(';
}
The function I call from a functions php file; 我从函数php文件中调用的函数;
function insertData($tablename, $columnName, $value)
{
$sql = 'INSERT into '.$tablename.'('.$columnName.') VALUES(:Value)';
$mysqlConnection = getConnection();
$statement = $mysqlConnection->prepare($sql);
$statement->bindValue(":Value", $value, PDO::PARAM_STR);
$bReturn = false;
try
{
$statement->execute();
$bReturn = true;
}
catch(PDOExecption $e)
{
echo $e->getMessage();
}
return $bReturn;
}
Does anyone know where I'm going wrong here? 有人知道我在哪里错吗?
if(isset($_POST['title, question, tags']))
Is not correct syntax 语法不正确
instead You can do: 相反,您可以执行以下操作:
if(isset($_POST['title']) && isset($_POST['question']) && isset($_POST['tags']))
or even 甚至
if(isset($_POST['title'], $_POST['question'], $_POST['tags']))
It would be easier to do execute it without binding: 无需绑定即可执行它:
insertData insertData
function insertData($tablename, $params){
//build query string
$column_string = implode(',', array_keys($params));
$value_string = implode(',', array_fill(0, count($params), '?'));
$sql_string = "INSERT INTO {$tablename} ({$columnString}) VALUES ({$value_string})";
//prepare query
$mysqlConnection = getConnection();
$statement = $mysqlConnection->prepare($sql_string);
//execute query
$success = $statement->execute(array_values($params));
//return boolean success
return $success;
}
But If you really need to bind, you can do it the following way: 但是,如果您确实需要绑定,则可以通过以下方式进行:
function insertDataBind($tablename, $params){
//build query string
$column_string = implode(',', array_keys($params));
$value_string = implode(',:', array_keys($params));
$sql_string = "INSERT INTO {$tablename} ({$column_string}) VALUES (:{$value_string})";
//prepare query
$mysqlConnection = getConnection();
$statement = $mysqlConnection->prepare($sql);
//bind
foreach($params as $key=>$value){
$statement->bindValue($key, $value);
}
//execute query
$success = $statement->execute();
//return boolean success
return $success;
}
usage: 用法:
if(isset($_POST['title'], $_POST['question'], $_POST['tags'])){
$params = array('title' => $_POST['title'],
'question'=>$_POST['question'],
'tags'=>$_POST['tags']
);
$success = insertData('questions', $params);
if(!$success)
echo 'Sorry failed :(';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.