我是pdo做的新手,并尝试将下面的mysqli代码转换为pdo服务器代码:
<?php
include "init.php";
session_start();
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
echo "<h2 align='center'>Your shopping cart is empty</h2>";
exit();
}
elseif (!isset($_SESSION["user_name"]) || count($_SESSION["user_name"]) < 1) {
$book=RAND(1000000, 2000000);
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item["item_id"];
$qty = $each_item["quantity"];
if($item_id =='sms')
{
$msg = $each_item["msg"];
$qty = 1;
}
mysqli_query($conn, "insert into books (book, item_name, quantity, msg) values ('$book', '$item_id', '$qty', '$msg')") or die(mysqli_error());
}
echo "<div class='info_post'>YOUR SHOPPING BOOKED CODE IS ' . $book . ' KINDLY COPY TO ANY DEALER NEAR YOU TO COMFIRM<br/ ></div><form action='mail.php' method='POST'><b> Mail me:</b><br/ >
<input type='text' name='book' size='23'> <input type='submit' name='submit' value='SEND EMAIL'></form>";
unset($_SESSION["cart_array"]);
}
elseif (isset($_SESSION["user_name"]) || count($_SESSION["user_name"]) > 1) {
$username=$_SESSION["user_name"];
$query=mysqli_query($conn, "select * from users where username='$username'");
if($row = mysqli_fetch_array($query))
{
$id=$row["id"];
$username=$row["username"];
$ip=$row["ip"];
$ban=$row["validated"];
$balance=$row["balance"];
$buy="$cartTotal";
{
if($ban != "0")
{
echo "<div class='info_post'><b>$buy $balance $ban</div>";
}
}
if($buy<$balance)
{
$redut = $balance-$buy;
$go = mysqli_query($conn,"update users set balance='$redut' where id='$id'");
if($go)
{
$book=RAND(1000000, 2000000);
foreach ($_SESSION["cart_array"] as $tem) {
$m = $tem["item_id"];
$p = $tem["quantity"];
$gob = mysqli_query($conn, "insert into books (book, item_name, quantity) values ('$book', '$m', '$p')") or die(mysqli_error());
$msg = "Transation of $totalquantity products cost of $cartTotal occur on your account with ticket id $book";
$time = date('Y-m-d H:i:s');
mysqli_query($conn, "insert into details (poster, message, date) values ('$username', '$msg', '$time')") or die(mysqli_error());
if($gob)
{
echo "jamjamja $book";
unset($_SESSION["cart_array"]);
}
else
{
echo "nootjo";
}
}
}
}
}
}
?>
尝试将所有代码更改为pdo服务器,这是我的代码看起来像代码的方式:
<?php
include "init.php";
session_start();
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
echo "<h2 align='center'>Your shopping cart is empty</h2>";
exit();
}
elseif (!isset($_SESSION["user_name"]) || count($_SESSION["user_name"]) < 1) {
$book=RAND(1000000, 2000000);
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item["item_id"];
$qty = $each_item["quantity"];
if($item_id =='sms')
{
$msg = $each_item["msg"];
$qty = 1;
}
$me = $conn->prepare("insert into books (book,item_name,quantity,msg) values (:book, :m, :p, :d)");
$me->bindParam(':book', $book);
$me->bindParam(':m', $item_id);
$me->bindParam(':p', $quantity);
$me->binParam(':d', $msg);
$me->execute();
}
echo "<div class='info_post'>YOUR SHOPPING BOOKED CODE IS ' . $book . ' KINDLY COPY TO ANY DEALER NEAR YOU TO COMFIRM<br/ ></div><form action='mail.php' method='POST'><b> Mail me:</b><br/ >
<input type='text' name='book' size='23'> <input type='submit' name='submit' value='SEND EMAIL'></form>";
unset($_SESSION["cart_array"]);
}
elseif (isset($_SESSION["user_name"]) || count($_SESSION["user_name"]) > 1) {
$username=$_SESSION["user_name"];
$records = $conn->prepare("select * from users where username= :book AND used = loading");
$records->bindParam(':book', $username);
$records->execute();
if($row = $records->fetch(PDO::FETCH_BOTH)){
$id=$row["id"];
$username=$row["username"];
$ip=$row["ip"];
$ban=$row["validated"];
$balance=$row["balance"];
if($ban != "0")
{
echo "<div class='info_post'><b>$buy $balance $ban</div>";
}
if($buy<=$balance)
{
$redut = $balance-$buy;
$go = $conn->prepare("update users set balance= :redut' where id= :id");
$go->bindParam(':redut', $redut);
$go->bindParam(':id', $id);
$go = $go->execute();
}
if($go)
{
$book=RAND(1000000, 2000000);
foreach ($_SESSION["cart_array"] as $tem) {
$m = $tem["item_id"];
$p = $tem["quantity"];
$gob = $conn->prepare("insert into books (book, item_name, quantity) values (:book, :m, :p)"); $gob->bindParam(':book', $book);
$gob->bindParam(':m', $m);
$gob->bindParam(':p', $p);
$gob = $gob->execute();
$msg = "Transation of $totalquantity products cost of $cartTotal occur on your account with ticket id $book";
$time = date('Y-m-d H:i:s');
$ty = $conn->prepare("insert into details (poster, message, date) values (:username, :msg, :time)");
$ty = bindParam(':username', $username);
$ty = bindParam(':msg', $msg);
$ty = bindParam(':time', $time);
if($gob)
{
echo "jamjamja $book";
unset($_SESSION["cart_array"]);
}
else
{
echo "nootjo";
}
}
}
}
}
?>
很遗憾地说,使用pdo方法时,我的代码在发送查询($ _SESSION [“user_name”])时不会输出结果要更新到我的数据库中,我无法弄清楚它为什么不输出结果导致我是一个新的pdo语句初学者我相信我在此之后会好起来的。
任何人都可以弄明白为什么我的代码会将记录插入数据库吗?
答案 0 :(得分:2)
我坚信使用正确的代码缩进可以更容易地找到错误 - 发布的代码很难“按原样”阅读,并且发现了一些小错误并在下面突出显示。
如果您在代码周围使用try/catch
块,您会发现更容易找到代码失败的特定点 - 常见的失败是prepared statements
因此使用的返回值prepare
方法作为抛出新异常的逻辑测试,如下所示。
希望有所帮助
<?php
include "init.php";
session_start();
try{
if( !isset( $_SESSION["cart_array"] ) || count( $_SESSION["cart_array"] ) < 1 ) {
throw new Exception("<h2 align='center'>Your shopping cart is empty</h2>");
} elseif( !isset( $_SESSION["user_name"] ) || count($_SESSION["user_name"]) < 1 ) {
$book = RAND(1000000, 2000000);
foreach($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item["item_id"];
$qty = $each_item["quantity"];
$msg=''; #<--------- value??
if( $item_id == 'sms' ) {
$msg = $each_item["msg"];
$qty = 1;
}
$me = $conn->prepare("insert into books (book,item_name,quantity,msg) values (:book, :m, :p, :d)");
if( !$me )throw new Exception('Failed to prepare sql statement',1);
$me->bindParam(':book', $book );
$me->bindParam(':m', $item_id );
$me->bindParam(':p', $qty ); #<-------- not quantity
$me->bindParam(':d', $msg ); #<-------- bindParam not binParam
$me->execute();
}
echo "
<div class='info_post'>YOUR SHOPPING BOOKED CODE IS ' . $book . ' KINDLY COPY TO ANY DEALER NEAR YOU TO COMFIRM<br/ ></div>
<form action='mail.php' method='POST'><b> Mail me:</b><br/ >
<input type='text' name='book' size='23'>
<input type='submit' name='submit' value='SEND EMAIL'>
</form>";
unset( $_SESSION["cart_array"] );
} elseif( isset($_SESSION["user_name"]) || count($_SESSION["user_name"]) > 1) {
$username = $_SESSION["user_name"];
$records = $conn->prepare("select * from users where username= :book AND used = loading"); #<--------- loading?? an unbound parameter perhaps?
if( !$records )throw new Exception('Failed to prepare sql statement',2);
$records->bindParam(':book', $username);
#$records->bindParam(':loading', $loading); #<---------- is there supposed to be a second bound parameter?
$records->execute();
if ( $row = $records->fetch(PDO::FETCH_BOTH) ) {
$id = $row["id"];
$username = $row["username"];
$ip = $row["ip"];
$ban = $row["validated"];
$balance = $row["balance"];
if ( $ban != "0" ) {
echo "<div class='info_post'><b>$buy $balance $ban</div>";
}
if ($buy <= $balance) {
$redut = $balance - $buy;
$go = $conn->prepare("update users set balance= :redut' where id=:id");
if( !$go ) throw new Exception('Failed to prepare sql statement',4);
$go->bindParam(':redut', $redut);
$go->bindParam(':id', $id);
$go = $go->execute();
}
if ($go) {
$book = RAND(1000000, 2000000);
foreach($_SESSION["cart_array"] as $tem) {
$m = $tem["item_id"];
$p = $tem["quantity"];
$gob = $conn->prepare("insert into books (book, item_name, quantity) values (:book, :m, :p)");
if( !$gob )throw new Exception('Failed to prepare sql statement',5);
$gob->bindParam(':book', $book);
$gob->bindParam(':m', $m);
$gob->bindParam(':p', $p);
$gob = $gob->execute();
$msg = "Transation of $totalquantity products cost of $cartTotal occur on your account with ticket id $book";
$time = date('Y-m-d H:i:s');
$ty = $conn->prepare("insert into details (poster, message, date) values (:username, :msg, :time)");
if( !$ty ) throw new Exception('Failed to prepare sql statement',6);
$ty = bindParam(':username', $username);
$ty = bindParam(':msg', $msg);
$ty = bindParam(':time', $time);
if ($gob) {
echo "jamjamja $book";
unset($_SESSION["cart_array"]);
}
else {
echo "nootjo";
}
}
}
} else {
throw new Exception('No records found',3);
}
}
} catch( Exception $e ){
printf( 'Error: Code %d Message %s', $e->getCode(), $e->getMessage() );
}
?>
在阅读完你的评论之后,再看看你原来的以及我之前提出的建议,并迅速将以下内容放在一起〜或许略微不同的逻辑,但我认为仍然遵循原文。
我尝试将prepare
方法调用移出任何循环(因为它们应该是)以及bindParam
调用,重新使用$stmt
对象名称,但确保{ {1}}对象在使用后关闭。
问题仍然存在 - sql中的$stmt
是什么 - 如果它是一个占位符,它需要采用什么值以及从哪个值派生出来?
loading