2017-06-14 26 views
0

我不知道爲什麼在第二輪的foreach給我這個錯誤產生的:錯誤的SQL語法,通過一個PHP的foreach

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '8,1)' at line 2"

$Ntavolo = Array ([0] => 46 [idordine] => 46) ; 
$queryordine= "SELECT `idordine` FROM `ordine` WHERE `tavolo`=$Ntavolo ORDER BY `ordine`.`dataora` DESC LIMIT 1"; 
$result = mysqli_query($con,$queryordine); 
$array=Array ([0] => Array ([id] => 1 [qta] => 1) [1] => Array ([id] => 8 [qta] => 1)) ; 
foreach($array as $value){ 
    $row = mysqli_fetch_array($result); 
     print_r($row); 
     print_r($array); 
    $idordine=$row['idordine']; 
    $queryinsert="INSERT INTO `dettaglio` (`iddettaglio`, `idordine`, `idprodotto`, `quantita`) 
    VALUES (NULL, ".$idordine.",".$value['id'].",".$value['qta'].");"; 
    mysqli_query($con,$queryinsert)or die(mysqli_error($con)); 
    echo($queryinsert); 
};?> 
+0

'$ row ['idordine']'也許是空的? – AbraCadaver

+0

或一個字符串.... –

+1

您已經開放SQL注入。由於您使用的是mysqli,請利用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[bind_param](http://php.net/手動/ EN/mysqli的-stmt.bind-param.php)。而且您不必擔心任何煩人的報價問題。 – aynber

回答

0
$queryinsert="INSERT INTO `dettaglio` (`iddettaglio`, `idordine`, `idprodotto`, `quantita`) 
VALUES (NULL, ".$idordine.",".$value['id'].",".$value['qta'].");"; 

這裏只有一個終止;應給予 您插入兩個終端

$queryinsert="INSERT INTO `dettaglio` (`iddettaglio`, `idordine`, `idprodotto`, `quantita`) 
    VALUES (NULL, ".$idordine.",".$value['id'].",".$value['qta'].")"; 
+0

其中一個';'是以文本字符串形式發送給MySQL的。另一個是用PHP代碼。當然,如果沒有設置多重查詢,MySQL';'會失敗,但這並不明顯,因爲來自MySQL的錯誤消息。 – ebyrob

+0

是的,但有一個終止點也會運行。錯誤是由於他使用的所有逗號而給出的 – coolmonster

+0

'$ hello =「我喜歡的一個詞;你好。」;'是完全有效的PHP否?那裏只有一個終結者沒有? – ebyrob

0

錯誤是在$行,第二個查詢$排不走價值... 謝謝大家!

$row = mysqli_fetch_array($result); 
$idordine=$row['idordine']; 
foreach($array as $value){ 
$queryinsert="INSERT INTO `dettaglio` (`iddettaglio`,`idordine`,`idprodotto`,`quantita`)VALUES (NULL,".$idordine.",".$value['id'].",".$value['qta'].")"; 
mysqli_query($con,$queryinsert)or die(mysqli_error($con));};