多个MYSQLi预备语句 [英] Multiple MYSQLi prepared statements

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

问题描述

我正在学习如何使用MYSQLi准备的语句,所以请放轻松.我可以让它们单独工作,但是当我将它们放在一起时它们将无法工作.

I am just learning how to use MYSQLi prepared statements so please go easy on me. I can get these to work individually but when I put them together they don't work.

$con = my connection details. 

这是我的查询代码:

// Insert the review into DB 
$stmt = $con->prepare("INSERT INTO reviews (order_id, comment) VALUES (?, ?)");
$stmt->bind_param('is', $order_id, $comment);
$stmt->execute(); 
$stmt->close();


// Update transactions to show review added
$stmt = $con->prepare("UPDATE transactions SET review = ? WHERE order_id = ?");
$stmt->bind_param('ii', 1, $order_id);
$stmt->execute(); 
$stmt->close(); 

在此先感谢您的帮助!非常感谢.

Thanks in advance for your help! Much appreciated.

推荐答案

您的代码不错,但是您不应使用相同的名称$stmt

your code is good however you should dont name them the same name $stmt

第一个陈述是$stmt

然后给第二个陈述另外一个名称,例如$stmt2.或您喜欢的名字.

then give second statment other name like $stmt2. or what ever name you like.

示例:

$stmt = $con->prepare("INSERT INTO reviews (order_id, comment) VALUES (?, ?)");
$stmt->bind_param('is', $order_id, $comment);
$stmt->execute(); 
$stmt->close();


// Update transactions to show review added
$stmt2 = $con->prepare("UPDATE transactions SET review = ? WHERE order_id = ?");
$stmt2->bind_param('ii', 1, $order_id);
$stmt2->execute(); 
$stmt2->close(); 

并调试您的代码,看看错误在哪里使用

and to debug your code and see where is the error use this.

if ($stmt = $con->prepare("INSERT INTO reviews (order_id, comment) VALUES (?, ?)") ){
   $stmt->bind_param('is', $order_id, $comment);
   $stmt->execute(); 
   $stmt->close();
    }
 else {printf("Error message:: %s\n", $con->error);}

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

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