Basically, I’d like to insert into table, getting values id from the same table,something like this
$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();
owever, this one does not work, anyone know why?
You could use
UNION:But it might be easier to first obtain the
MAX(idfichecmd)in PHP, then useINSERT ... VALUES(remembering to perform the operation in a transaction in order to preserve atomicity):Or, if you are merely looking to give each new record an incremented
idfichecmd, consider making the columnAUTO_INCREMENT(whereby MySQL will do all of this for you automatically):