我尝试在我的数据库中更新或插入订单,但我有一个问题,因为我有2个ID第一个(id_commande - 自动增量)的订单,第二个是与其他表相关的成员。
我找不到如何调用id_commande。
<?php
session_start();
// on teste si le visiteur a soumis le formulaire
if (isset($_POST['commande']) && $_POST['commande'] == 'commande') {
commander('homme', 'id_commande');
}
else if (isset($_POST['commande_femme']) && $_POST['commande_femme'] == 'commande_femme') {
commander('femme', 'id_commande');
}
function commander($commande,$id_commande){
// $id = $_SESSION['id'];
// Connection au serveur
$dns = 'mysql:host=localhost;dbname=carole';
$utilisateur = 'acces_site';
$motDePasse = 'password';
$connection = new PDO( $dns, $utilisateur, $motDePasse );
$req = $connection->prepare('SELECT * FROM commande WHERE id_commande = ?');
$req->execute(array($id_commande));
$stmt = $req->rowCount();
if ($stmt == 1){
// SI IL EXISTE 1 SINON 0
$sql = "UPDATE commande SET commande = :commande, quantite_commande = :quantite_commande WHERE id = :id";
$stmt = $connection->prepare($sql);
$stmt->bindParam(':commande', $commande);
$stmt->bindParam(':quantite_commande', 'quantite_'.$commande);
$stmt->execute(array(':commande' => $_POST[$commande], ':quantite_commande' => $_POST['quantite_'.$commande], ':id' => $id ));
}
else {
$sql = 'INSERT INTO commande (id,commande,quantite_commande) VALUES (:id, :commande, :quantite_commande)';
$stmt = $connection->prepare($sql);
$stmt->bindParam(':commande', $commande);
$stmt->execute(array(':id' => $id, ':commande' => $_POST[$commande], ':quantite_commande' => $_POST['quantite_'.$commande]));
}
}
我需要你的帮助! THX
答案 0 :(得分:0)
也许你需要使用PDO::lastInsertId吗?
答案 1 :(得分:0)
在这一行:
$stmt->execute(array(':commande' => $_POST[$commande], ':quantite_commande' => $_POST['quantite_'.$commande], ':id' => $id ));
如果你之前没有定义它,你是如何使用$ id的?!?!