2016-10-28 9 views
1

[ASK] my php error "失敗したクエリSQL構文にエラーがあります。使用する正しい構文については、MariaDBサーバのバージョンに対応するマニュアルをチェックしてください(iduser、idfood、jumlah、total)VALUES(9,1,2,20000) 'at line 1 " 何が間違っていますか?このPHPから値の成功とメッセージを取得する方法を失敗したクエリSQL構文にエラーがあります

myphp

<?php 
$data= file_get_contents('php://input'); 
    $array = json_decode($data, true); 

    require_once ('..../db_connectfood.php'); 

    $db = new DB_CONNECT(); 

    foreach($array as $row) 
    { 
     $idusr = $row["iduser"]; 
     $idfd = $row["idfood"]; 
     $jml = $row["jumlah"]; 
     $total = $row["total"]; 
     $result = mysql_query("INSERT INTO order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());   

    } 
    if ($result) { 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = "successfully created."; 

     // echoing JSON response 
     echo json_encode($response); 
    } else { 
     // failed to insert row 
     $response["success"] = 0; 
     $response["message"] = "Oops! An error occurred."; 

     // echoing JSON response 
     echo json_encode($response); 
    } 

?> 

enter image description here

iduserとidfoodこんにちは、外部キー

+1

テーブルに「iduser」という名前の列がありますか? mysqlマネージャーからそのクエリを実行すると動作しますか? – Jason

答えて

0

でこれを試してください:

$結果=するmysql_query( "databasename.order。INSERT INTO(iduser、idfood、jumlah、合計)VALUES ($ idusr、$ idfd、$ jml、$ total) ")またはdie("失敗したクエリ ".mysql_error());

orderはmysqlの命名規則です。

お待ちしております。

+0

ありがとうの仕事 –

1

orderはSQLの予約語です。最適な方法は、テーブルの名前を変更することです(例:orders、複数)。これが不可能な場合は、名前を逃れるためにバッククォートを使用することができます。

INSERT INTO `order` (iduser, idfood, jumlah, total) 
VALUES ($idusr, $idfd, $jml, $total) 

必須コメント:
文字列操作を使用したSQL文のSQLインジェクション攻撃に対して脆弱あなたのコードを残します。代わりにprepared statementの使用を検討する必要があります。

+0

私にそれを打つ。 :) – Shef

+0

ありがとうございました –

0

orderは、reserved wordです。

予約語をクエリで使用できるようにするには、 `予約語`のようにバッククォートで囲んでエスケープする必要があります(バッククォートは一重引用符のように見えますが、そうではありません)。

だから、あなたのケースの変化

$result = mysql_query("INSERT INTO order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error()); 

$result = mysql_query("INSERT INTO `order` (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error()); 

に...そして、彼らが予約語になっている場合は、テーブルまたは列の名前の残りの部分を確認してください。

&ベストプラクティスとして、パラメータ化されたクエリ/プリペアドステートメントが推奨されます。

+0

ありがとうございました –

+0

@SomaKun:それは助けてうれしいです。 ** [あなたの質問の回答の1つを受け入れる**](http://meta.stackexchange.com/a/5235/165614)を自由に感じてください。 – Shef

関連する問題