2013-04-14 127 views
-4

我是PHP和SQL的新手,出於某種原因,我得到了這個非常奇怪和神祕的錯誤。奇怪的SQL錯誤,PHP

Fatal error: 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 'INSER INTO history(stype, symbol, amount, price, time, userid) VALUES (?, ?, ?, ' at line 1 in /home/jharvard/vhosts/localhost/includes/functions.php on line 139

,這是我使用的代碼:

<?php 
require("../includes/config.php"); 
if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
    // Retrieve the requested user's symbol and calculate the stock's price 
    $symbol = $_POST["symbol"]; 
    $price = lookup($symbol)["price"]; 
    // If lookup fails, apologize 
    if ($price === null) { 
    apologize("Sorry!");} 
    // Retrieve user's cash balance 
    $cash = query("select cash from users where id = ?", $_SESSION["id"]); 
    foreach ($cash as $qux){$cash = $qux["cash"];} 
    // Calculate the amount of money needed to buy the amount of stock 
    $PayPrice = $_POST["SAmount"] * $price; 
    // If user doesnt have enough cash, apologize 
    if ($cash < $PayPrice){apologize("Your a cheapskate");} 
    // If user enters a decimal or negative number, apologize 
    if (preg_match("/^\d+$/", $_POST["SAmount"]) != true){apologize("Y u so decimal");} 
    // Deduct the amount of cash from user's money 
    $result = query("UPDATE users SET cash = ? where id = ?", ($cash - $PayPrice), $_SESSION["id"]); 
    if ($result === false){apologize("Update failed, cash");} 
    // Update the user's stock 
    $result = query("INSERT INTO stocks (id, symbol, stock) VALUES (?,?,?) ON DUPLICATE KEY UPDATE stock = stock + ?", $_SESSION['id'], $_POST["symbol"], $_POST["SAmount"], $_POST["SAmount"]); 
    if ($result === false){apologize("Update failed, stock");} 
    // Update history 
    $result = query("INSER INTO history(stype, symbol, amount, price, time, userid) VALUES (?, ?, ?, ?, ?, ?)", 'buy', $symbol, $_POST['SAmount'], $PayPrice, time(), $_SESSION['id']); 

} 
else{render("BuyPage.php");} 

?> 

,這裏是查詢()代碼

function query(/* $sql [, ... ] */) 
    { 
     // SQL statement 
     $sql = func_get_arg(0); 

     // parameters, if any 
     $parameters = array_slice(func_get_args(), 1); 

     // try to connect to database 
     static $handle; 
     if (!isset($handle)) 
     { 
      try 
      { 
       // connect to database 
       $handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD); 

       // ensure that PDO::prepare returns false when passed invalid SQL 
       $handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
      } 
      catch (Exception $e) 
      { 
       // trigger (big, orange) error 
       trigger_error($e->getMessage(), E_USER_ERROR); 
       exit; 
      } 
     } 

     // prepare SQL statement 
     $statement = $handle->prepare($sql); 
     if ($statement === false) 
     { 
      // trigger (big, orange) error 
      trigger_error($handle->errorInfo()[2], E_USER_ERROR); 
      exit; 
     } 

     // execute SQL statement 
     $results = $statement->execute($parameters); 

     // return result set's rows, if any 
     if ($results !== false) 
     { 
      return $statement->fetchAll(PDO::FETCH_ASSOC); 
     } 
     else 
     { 
      return false; 
     } 
    } 

這個問題很奇怪神祕的,因爲我不知道是什麼繼續,我包括了所有我認爲需要的代碼,但如果需要更多的代碼,請告訴我。 Ps:不用說,這個問題只有在我輸入SQL代碼的最後一行插入到歷史記錄之後纔會發生,否則其他一切工作都很好。

+11

不錯不錯,很* *神祕。 'INSER INTO' - 我的建議是,你從現在開始更仔細地閱讀錯誤信息。 –

+0

專業提示:當錯誤消息表明您的SQL語法中有錯誤時,請考慮可能的解釋,即您的SQL語法中有錯誤。 (我會承認這些消息在問題出現的地方並不精確) –

回答

2

替換:

$result = query("INSER INTO history(stype, symbol, amount, price, time, userid) VALUES (?, ?, ?, ?, ?, ?)", 'buy', $symbol, $_POST['SAmount'], $PayPrice, time(), $_SESSION['id']); 

有了:

$result = query("INSERT INTO history(stype, symbol, amount, price, time, userid) VALUES (?, ?, ?, ?, ?, ?)", 'buy', $symbol, $_POST['SAmount'], $PayPrice, time(), $_SESSION['id']); 
+2

如果你想知道他改變了什麼,你應該把'INSER'而不是'INSERT'。 – Mattios550