0
一つの形で複数のデータベース行の挿入、同じUSER_IDとORDER_IDが、異なるPRODUCT_ID、私は取得していますエラーとは、それぞれ次のとおりです。私は、データベースに複数の行を挿入しようとしている
SQLSTATE [23000 ]:整合性制約違反:1048列 'のproduct_id' をNULLにすることはできません
ボタン機能:
if(isset($_POST['confirm_order']))
{
$product_id = array($_POST['productid']);
$user_id = $_POST['userid'];
$user->confirm_order($product_id,$user_id);
}
フォーム:
<?php
$stmt = $DB_con->prepare("SELECT * FROM products as p INNER JOIN basket as b ON p.product_id=b.product_id WHERE user_id = :user_id");
$stmt->bindParam(":user_id",$user_id);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($row as $p)
{
echo '<form method="POST" enctype="multipart/form-data">
<tr>
<td><img id="img" class="img-fluid" src="images/',$p['product_image'],'" style="height:100px;width:100px;"></td>
<td>',$p['product_name'],'</td>
<td>£',$p['product_price'],'</td>
<td>
<input type="hidden" id="productid" name="productid[]" value="',array($p['product_id']),'">
<input type="hidden" id="userid" name="userid" value="',$user_id,'">
</td>
</tr>';
}
?>
<button type="submit" class="btn btn-primary" name="confirm_order"> Confirm Order</button>
</form>
機能:
public function confirm_order($product_id,$user_id)
{
try
{
$order_id = substr(str_shuffle(MD5(microtime())), 0, 10);
$stmt1 = $this->db->prepare("INSERT INTO orders (order_id, product_id, user_id) VALUES (:order_id, :product_id, :user_id)");
$stmt1->bindparam(":order_id", $order_id);
$stmt1->bindparam(":product_id", $product_id[]);
$stmt1->bindparam(":user_id", $user_id);
$stmt1->execute();
$stmt2 = $this->db->prepare("DELETE * FROM basket WHERE product_id = :product_id AND user_id = :user_id");
$stmt2->bindparam(":product_id", $product_id[]);
$stmt2->bindparam(":user_id", $user_id);
$stmt2->execute();
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
どうすればよいですか?行を数え、すべての行が入るまで()を実行します。ループではそれほど良いことではありません、ごめんなさい – Aaranihlus