PDO多插入语句 [英] PDO multi insert statement

查看:56
本文介绍了PDO多插入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为单个输入字段提供了一个有效的插入内容,但是当我尝试添加更多内容时,似乎会破坏所有内容.我的数据库连接工作正常,当我将"isset"更改为中断的三列时,页面连接正确包含在页面顶部.

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.

这是我的既定声明;

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 :(';
}

我从函数php文件中调用的函数;

The function I call from a functions php file;

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']))

语法不正确

相反,您可以这样做:

if(isset($_POST['title']) && isset($_POST['question']) && isset($_POST['tags']))

甚至

if(isset($_POST['title'], $_POST['question'], $_POST['tags']))


在不绑定的情况下执行起来会更容易:


It would be easier to do execute it without binding:

插入数据

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;
}

用法:

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 :(';
}

这篇关于PDO多插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆