2013-04-15 131 views
0

任何人都可以闡明爲什麼這個PHP/MySQL不工作?基本上,我需要一次從表格中插入行的負載,所以將有多個名稱字段,多個短,中,長字段等即時得到這個錯誤:一次向MySQL中插入多個值

Notice: Undefined variable: Short1 in C:\xampp\htdocs\process.php on line 95 
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 'Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year) VAL' at line 2 

這是我的PHP

<?php 


$host = "localhost"; 
$databasename = "pe_results"; 
$databaseusername = "root"; 
$databasepassword = ""; 

$conn = mysql_connect("$host", "$databaseusername", "$databasepassword"); 
mysql_select_db("$databasename", $conn); 

     if (isset($_POST['Name1'])) { 
     $Name1 = $_POST['Name1']; 
     } 
     if (isset($_POST['Short1'])) { 
     $Short1 = $_POST['Short1']; 
     } 
     if (isset($_POST['Med1'])) { 
     $Med1 = $_POST['Med1']; 
     } 
     if (isset($_POST['Long1'])) { 
     $Long1 = $_POST['Long1']; 
     } 
     if (isset($_POST['VLong1'])) { 
     $VLong1 = $_POST['VLong1']; 
     } 
     if (isset($_POST['Extreme1'])) { 
     $Extreme1 = $_POST['Extreme1']; 
     } 
     if (isset($_POST['LJump1'])) { 
     $LJump1 = $_POST['LJump1']; 
     } 
     if (isset($_POST['HJump1'])) { 
     $HJump1 = $_POST['HJump1']; 
     } 
     if (isset($_POST['Shotputt1'])) { 
     $Shotputt1 = $_POST['Shotputt1']; 
     } 
     if (isset($_POST['Discuss1'])) { 
     $Discuss1 = $_POST['Discuss1']; 
     } 
     if (isset($_POST['Javelin1'])) { 
     $Javelin1 = $_POST['Javelin1']; 
     } 
     if (isset($_POST['Date'])) { 
     $Date = $_POST['Date']; 
     } 
     if (isset($_POST['Year'])) { 
     $Year = $_POST['Year']; 
     } 
     // Sector 2 */ 
      if (isset($_POST['Name2'])) { 
     $Name2 = $_POST['Name2']; 
     } 
     if (isset($_POST['Short2'])) { 
     $Short2 = $_POST['Short2']; 
     } 
     if (isset($_POST['Med2'])) { 
     $Med2 = $_POST['Med2']; 
     } 
     if (isset($_POST['Long2'])) { 
     $Long2 = $_POST['Long2']; 
     } 
     if (isset($_POST['VLong2'])) { 
     $VLong2 = $_POST['VLong2']; 
     } 
     if (isset($_POST['Extreme2'])) { 
     $Extreme2 = $_POST['Extreme2']; 
     } 
     if (isset($_POST['LJump2'])) { 
     $LJump2 = $_POST['LJump2']; 
     } 
     if (isset($_POST['HJump2'])) { 
     $HJump2 = $_POST['HJump2']; 
     } 
     if (isset($_POST['Shotputt2'])) { 
     $Shotputt2 = $_POST['Shotputt2']; 
     } 
     if (isset($_POST['Discuss2'])) { 
     $Discuss2 = $_POST['Discuss2']; 
     } 
     if (isset($_POST['Javelin2'])) { 
     $Javelin2 = $_POST['Javelin2']; 
     } 
     if (isset($_POST['Date'])) { 
     $Date = $_POST['Date']; 
     } 
     if (isset($_POST['Year'])) { 
     $Year = $_POST['Year']; 
     } 

     $sql="INSERT INTO results_main 
    (Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year) 
VALUES 
    ('$Name1', '$Short1', '$Med1', '$Long1', '$VLong1', '$Extreme1', '$LJump1', '$HJump1', '$Shotputt1', '$Discuss1', '$Javelin1', '$Date', '$Year'), 
    ('$Name2', '$Short2', '$Med2', '$Long2', '$VLong2', '$Extreme2', '$LJump2', '$HJump2', '$Shotputt2', '$Discuss2', '$Javelin2', '$Date', '$Year'); 
"; 

$result = mysql_query($sql) or die(mysql_error()); 

// close connection 
mysql_close($conn); 

?> 

爲JW

Notice: Undefined variable: Short1 in C:\xampp\htdocs\process.php on line 95 
INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`) VALUES (`1`, ``, `1`, `1`, `1`, `1`, `1`, `1`, `1`, `1`, `1`, `2013-04-26`, `10`), (`2`, `2`, `2`, `2`, `2`, `2`, `2`, `2`, `2`, `2`, `2`, `2013-04-26`, `10`); Unknown column '1' in 'field list' 

回答

3

LONG新的錯誤消息是保留關鍵字,恰好是你列的名稱。爲了避免語法錯誤,列名應該使用反引號進行轉義。

INSERT INTO results_main(Name, Short, Med, `Long`, VLong, ...) VALUES (....) 

如果您有權限變更列,將名稱更改爲一個非保留關鍵字,以避免問題找回未來。


作爲旁註,查詢是用SQL Injection脆弱,如果變量的值(小號)從外部來了。請看下面的文章,瞭解如何防止它。通過使用PreparedStatements你可以擺脫使用單引號圍繞值。

+0

謝謝,我用反引號排序了這個問題,但現在我得到這個錯誤:注意:未定義的變量:Short1在95行的C:\ xampp \ htdocs \ process.php中 '字段列表中的未知列'1' ' –

+0

在調用'mysql_query()'之前,你能發佈這個結果嗎:'echo $ sql'? –

+0

更新了問題的結果,我已經解決了調用$ Short1的問題,但是我仍然收到未知列1錯誤 –

0

檢查後陣列,你有什麼毛病$ _ POST [ 'Short1'];

另外不要使用MYSQL保留關鍵字,Long是保留關鍵字。如果你使用你應該逃脫它

`Long` 
+0

短變量是好的,我不知道爲什麼它踢起來大驚小怪,一切工作正常,唯一不同的是名稱。此外,我已經將所有內容都包含在反引號中,因此不必擔心保留字 –

0

我建議通過傾銷你的$ _POST陣列,看着它。根據你的代碼,只有在$ _POST數組中有一個值時纔會設置變量。