2012-09-15 21 views
2

我用這個例子:www.jtable.orgMysql查詢不能在編輯的jTable代碼中工作,爲什麼?

我已經下載了JTable的PHP版本。然後我編輯了腳本。 jTable簡單版本正在工作,但我的編輯版本不是

我可以創建一個列表,但我不能添加一行;此代碼導致問題。但是,PHP不顯示任何錯誤消息。

else if($_GET["action"] == "create") 
{ 
    //Insert record into database 
    $result = mysql_query("INSERT INTO veriler(bolge, sehir, firma, adres, tel, web) VALUES('" . $_POST["bolge"] . "', '" . $_POST["sehir"] . "', '" . $_POST["firma"] . "', '" . $_POST["adres"] . "', '" . $_POST["tel"] . "', '" . $_POST["web"] . "'"); 

    //Get last inserted record (to return to jTable) 
    $result = mysql_query("SELECT * FROM veriler WHERE id = LAST_INSERT_ID();"); 
    $row = mysql_fetch_array($result); 


    //Return result to jTable 
    $jTableResult = array(); 
    $jTableResult['Result'] = "OK"; 
    $jTableResult['Record'] = $row; 

    print json_encode($jTableResult); 
} 

什麼問題?

+0

嘗試添加該頂端顯示的錯誤一個查詢: '的error_reporting(E_ALL^E_NOTICE); ini_set('display_errors','1');' – Erik

+0

迴應你的SQL語句的內容,並在數據庫中運行它們?做他們的工作?另外,你應該考慮移動到'PDO'或'mysqli_',因爲它們讓你編寫更安全的代碼。 – andrewsi

回答

10

在這條線的問題,還有一個問題:

$result = mysql_query("INSERT INTO veriler(bolge, sehir, firma, adres, tel, web) VALUES('" . $_POST["bolge"] . "', '" . $_POST["sehir"] . "', '" . $_POST["firma"] . "', '" . $_POST["adres"] . "', '" . $_POST["tel"] . "', '" . $_POST["web"] . "'"); 

的INSERT查詢格式:

INSERT INTO table (column1, column2, etc) VALUES (value1, value2, etc); 

您錯過了VALUES部分的右括號。

要提高你的代碼,你可以做這樣的事情:

$result = mysql_query("YOUR QUERY") or die('ERROR: '.mysql_error()); 

並請在SQL Injection閱讀。

+0

www.jtable.org是不是我自己的代碼寫。如果你想要一個例子,請看jtable.org下載類。感謝幫助! – Ryliatron

+0

現在我嘗試了你的修復,並努力工作。謝謝!你是個大個子! – Ryliatron

3

這裏是你忘了)

$result = mysql_query("INSERT INTO veriler(bolge, sehir, firma, adres, tel, web) 
         VALUES('" . $_POST["bolge"] . "', '" . $_POST["sehir"] . "', '" . $_POST["firma"] . "', '" . $_POST["adres"] . "', '" . $_POST["tel"] . "', '" . $_POST["web"] . "'"); 

使用

$result = mysql_query("INSERT INTO veriler(bolge, sehir, firma, adres, tel, web) VALUES 
         ('{$_POST["bolge"]}', '{$_POST["sehir"] }' , '{$_POST["firma"]}' , '{$_POST["adres"] }', '{$_POST["tel"]}', '{$_POST["web"]}')") ; 
+0

謝謝你的回覆,但它的真實,當我刪除「;」這是給錯誤並且不加載列表數據。 我使用ajax代碼,它不給mysql的錯誤.. – Ryliatron

+1

@FurkanKadıoğlu對不起,我錯了現在檢查這個出來 –

2

首先你可以減少last_inset_id()

else if($_GET["action"] == "create") 
{ 
    //Insert record into database 
    $result = mysql_query("INSERT INTO veriler(bolge, sehir, firma, adres, tel, web) VALUES('" . $_POST["bolge"] . "', '" . $_POST["sehir"] . "', '" . $_POST["firma"] . "', '" . $_POST["adres"] . "', '" . $_POST["tel"] . "', '" . $_POST["web"] . "'")); 
//Get last inserted record (to return to jTable) 
//check youe result query you are missing something here 
$id=mysql_insert_id(); 
//this will automatically give you last id 

//Return result to jTable 
$jTableResult = array(); 
$jTableResult['Result'] = "OK"; 
$jTableResult['id'] = $id; 
$jTableResult['Record'] = $row; 
$jTableResult['aderes'] = $_POST['adres']; 
//and so on 
print json_encode($jTableResult); 

}

相關問題