mysqli使用变量准备和插入语句以插入值和变量语法错误

时间:2014-07-12 00:58:56

标签: php mysql mysqli

您好我正在尝试将记录插入我的数据库。字段数是可变的,取决于用户表单提交。使用以下代码

完美地插入核心数据
 if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {


    if ($insert_stmt = $mysqli->prepare("INSERT INTO submitted_orders (order_date,customer_id,customer_email,customer_name,customer_company,customer_message,country_name,city_name,site) VALUES (?, ?, ?, ?, ?, ?, ?,?, ?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;

   //   
};
       if ( $insert_stmt->bind_param('sisssssss', $today,$_SESSION['customer_id'],$_SESSION['email'],$_SESSION['name'],$_SESSION['company_name'],$_SESSION['message'],$_SESSION['country_name'],$_SESSION['city_name'],$_SESSION['site'])) {    echo "Binding parameters failed: (" . $insert_stmt->errno . ") " . $insert_stmt->error;};
        // Execute the prepared query.
         if (!$insert_stmt->execute()) {
echo "Execute failed: (" . $insert_stmt->errno . ") " . $insert_stmt->error;
 }

所以我的问题是我有四个定义的变量

$ scounter(这包含我的额外字段的字符串整数值,echo测试给出的值为sisisi

$ qcounter(这包含要输入的字段数,echo显示的值为?,?,?,?,?,?,

$ input1(这包含所需的数据库字段列表,ech显示测试值为order_item1,order_item1_price,order_item2,order_item2_price,order_item3,order_item3_price,

$ sinput2(这包含要输入值的变量echo显示测试值为$ order_item1,$ order_item1_price,$ order_item2,$ order_item2_price,$ order_item3,$ order_item3_price,

我需要将变量内容输入到prepare和bind参数语句中,但需要进行拼写以获得正确的语法并继续获取"致命错误:调用非对象上的成员函数bind_param()第141行" /hsphere/local/home/mailman2/afbincludes/order_form_handler.php;消息

我真的很感激一些帮助,使语法正确,替代方案是添加60个字段,其中大部分都有空数据

这是我尝试插入变量的代码

if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {
//

// post data // Insert the new user into the database 
if ($insert_stmt = $mysqli->prepare("INSERT INTO submitted_orders ('".$input1.  "',order_date,customer_id,customer_email,customer_name,customer_company,customer_message,country_name,city_name,site) VALUES ('".$qcounter."'?, ?, ?, ?, ?, ?, ?,?, ?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
//

//  
}; 
if ( $insert_stmt->bind_param($scounter.'sisssssss',$input2, $today,$_SESSION['customer_id'],$_SESSION['email'],$_SESSION['name'],$_SESSION['company_name'],$_SESSION['message'],$_SESSION['country_name'],$_SESSION['city_name'],$_SESSION['site'])) {    echo "Binding parameters failed: (" . $insert_stmt->errno . ") " . $insert_stmt->error;};
        // Execute the prepared query.
         if (!$insert_stmt->execute()) {
echo "Execute failed: (" . $insert_stmt->errno . ") " . $insert_stmt->error;
 }

1 个答案:

答案 0 :(得分:1)

如果您回复了查询,则会看到语法错误。

if ($insert_stmt = $mysqli->prepare("INSERT INTO submitted_orders ('".$input1.  "',order_date,customer_id,customer_email,customer_name,customer_company,customer_message,country_name,city_name,site) VALUES ('".$qcounter."'?, ?, ?, ?, ?, ?, ?,?, ?)"))

应该是

if ($insert_stmt = $mysqli->prepare("INSERT INTO submitted_orders (".$input1.  " order_date,customer_id,customer_email,customer_name,customer_company,customer_message,country_name,city_name,site) VALUES (".$qcounter." ?, ?, ?, ?, ?, ?, ?,?, ?)"))

',之后/之后删除单引号$input1和逗号$qcounter。由于您的列名和占位符不应该在引号中,并且它们的值都有一个尾随逗号。

另外,

if ( $insert_stmt->bind_param($scounter.'sisssssss',$input2, $today,$_SESSION['customer_id'],$_SESSION['email'],$_SESSION['name'],$_SESSION['company_name'],$_SESSION['message'],$_SESSION['country_name'],$_SESSION['city_name'],$_SESSION['site']))

应该是

if ( $insert_stmt->bind_param($scounter.'sisssssss',rtrim($input2, ','),  $today,$_SESSION['customer_id'],$_SESSION['email'],$_SESSION['name'],$_SESSION['company_name'],$_SESSION['message'],$_SESSION['country_name'],$_SESSION['city_name'],$_SESSION['site']))

删除,

中的尾随逗号$input2