0

基本的に、同じテーブルから値IDを取得して、テーブルに挿入したいと思います。

$sql = ("INSERT INTO commande (idfichecmd,idproduit,idclt,qte,datecmd)
(SELECT MAX(idfichecmd)+1 ,'1' AS idproduit ,'$idC' AS idclt ,'$fb' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'2' AS idproduit ,'$idC' AS idclt ,'$g' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'3' AS idproduit ,'$idC' AS idclt ,'$pi' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'4' AS idproduit ,'$idC' AS idclt ,'$ft' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'5' AS idproduit ,'$idC' AS idclt ,'$sd' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'6' AS idproduit ,'$idC' AS idclt ,'$ad' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'7' AS idproduit ,'$idC' AS idclt ,'$vs' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'8' AS idproduit ,'$idC' AS idclt ,'$wi' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'9' AS idproduit ,'$idC' AS idclt ,'$equip' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'10' AS idproduit ,'$idC' AS idclt ,'$fo' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'11' AS idproduit ,'$idC' AS idclt ,'$fh' AS qte,'$date' FROM commande)")or die(mysql_error());
$result = mysql_query($sql) or die("Unable to add commande : ". mysql_error());   
mysql_close();

しかし、これは機能しません、誰もが理由を知っていますか?

4

1 に答える 1

0

あなたが使用することができますUNION

INSERT INTO commande (idfichecmd, idproduit, idclt, qte, datecmd)
  SELECT MAX(idfichecmd)+1,  1, '$idC', '$fb'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  2, '$idC', '$g'    , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  3, '$idC', '$pi'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  4, '$idC', '$ft'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  5, '$idC', '$sd'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  6, '$idC', '$ad'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  7, '$idC', '$vs'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  8, '$idC', '$wi'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  ,  9, '$idC', '$equip', '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  , 10, '$idC', '$fo'   , '$date' FROM commande
UNION ALL
  SELECT MAX(idfichecmd)  , 11, '$idC', '$fh'   , '$date' FROM commande

MAX(idfichecmd)ただし、最初にPHPで取得してから、次を使用する方が簡単な場合がありますINSERT ... VALUES(アトミック性を維持するために、トランザクションで操作を実行することを忘れないでください)。

INSERT INTO commande (idfichecmd, idproduit, idclut, qte, datecmd) VALUES
  ($id+1, 1, '$idC', '$fb', '$date'),
  ($id  , 2, '$idC', '$g' , '$date'),
  -- etc.

または、単に新しい各レコードに増分を与えることをidfichecmd検討している場合は、列を作成することを検討してAUTO_INCREMENTください(これにより、MySQLがこれらすべてを自動的に実行します)。

ALTER TABLE commande MODIFY idfichecmd INT AUTO_INCREMENT;
INSERT INTO commande (idproduit, idclut, qte, datecmd) VALUES
  (1, '$idC', '$fb', '$date'),
  (2, '$idC', '$g' , '$date'),
  -- etc.
于 2012-05-31T10:55:35.790 に答える