PHP中未保存到数据库的变量

PHP中未保存到数据库的变量,php,mysql,phpmyadmin,Php,Mysql,Phpmyadmin,我的一段代码有问题。它工作得非常好,只是它不会将自己保存到数据库中。代码如下: 函数createOrder($user、$cart、$price、$method){ 试一试{ $username=“root”; $password=“”; $connection=new-PDO('mysql:host=localhost;dbname=dreamlineslaapsystem',$username,$password); $connection->beginTransaction(); $pr

我的一段代码有问题。它工作得非常好,只是它不会将自己保存到数据库中。代码如下:

函数createOrder($user、$cart、$price、$method){
试一试{
$username=“root”;
$password=“”;
$connection=new-PDO('mysql:host=localhost;dbname=dreamlineslaapsystem',$username,$password);
$connection->beginTransaction();
$productList=$\会话['products'];
$orderList=$\会话['orders'];
$orderItems=$\会话['orderItems'];
$orderid=generateOrderid();
$allOrders=array();
对于($i=0;$igetID());
}
while(在数组中($orderid,$allOrders)){
$orderid=generateOrderid();
}
$today=日期(“Y-m-d H:i:s”);
$order=新订单($orderid,$user->getID(),$price,$today,$method);
$newOrder=数组(
':id'=>$orderid,
':userid'=>$user->getID(),
“:date”=>今天$,
':method'=>$method
);
$addOrder=$connection->prepare('INSERT-INTO-orders(id,userid,date)值(:id,:userid,:date,:method');
$addOrder->execute($newOrder);
数组推送($orderList,$order);
foreach($cart->getCart()作为$item=>$amount){
$itemid=null;
对于($i=0;$igetID()==$item){
$orderitem=新的orderitem($orderid,$i,$amount);
数组\u push($orderItems,$orderitem);
$newOrderitem=数组(
':orderid'=>$orderid,
“:productid”=>$i,
“:金额”=>美元金额
);
$addOrderitem=$connection->prepare('INSERT-INTO-orderitems(orderid,productid,amount)值(:orderid,:productid,:amount');
$addOrderitem->execute($newOrderitem);
}
}
}
$connection->commit();
$\会话['orders']=$orderList;
$\会话['orderitems']=$orderitems;
返回$orderid;
}捕获(PDO$e){
$connection->rollback();
打印“Er is iets fout gegaan:”.e->getMessage()。“
”; 返回null; } }

它确实会将所有内容添加到数组和会话中,当我执行
var_dump
时,我会查看这些内容是否都正确存储在会话/数组中。它只是不会添加到数据库中。

您有3列,但插入了4个值。我假设您的表中有方法列,并且您的insert语句没有关闭括号

$addOrderitem = $connection->prepare('INSERT INTO orderitems(orderid, productid, amount, method) VALUES (:orderid, :productid, :amount, :method'));


$addOrderitem = $connection->prepare('INSERT INTO orderitems(orderid, productid, amount) VALUES (:orderid, :productid, :amount'));

插入订单(id,userid,date)值(:id,:userid,:date,:method'))
第一个括号包含3个值,第二个括号包含4个值。您应该打开PDO错误信号,然后您将从查询中的所有问题中得到一个错误。为什么您认为列列表中的
方法
是一个输入错误?他在
$newOrder
中有
':method'=>$method
。您不应该将
:method
添加到中吗ode>VALUES而不是将
method
从列中删除?谢谢,我真不敢相信我遗漏了这样的小东西。我已经发现它很奇怪了。无论如何-非常感谢!:)我后来添加了这个方法,所以我也错过了一次添加。现在一切都已修复并正常工作。@user3788474如果答案有帮助,请接受它以表明您的问题已解决。如果不是,就让它保持原样。祝你好运。
$addOrderitem = $connection->prepare('INSERT INTO orderitems(orderid, productid, amount, method) VALUES (:orderid, :productid, :amount, :method'));


$addOrderitem = $connection->prepare('INSERT INTO orderitems(orderid, productid, amount) VALUES (:orderid, :productid, :amount'));