2016-06-08 49 views
2

儘管我非常肯定我的代碼是好的(做過幾次測試),但數據庫並未更新。所以必須有一個問題(異步等)。我需要幫助來弄清楚什麼是錯的。通過Ajax發佈到MySql的問題

這裏是我的Ajax調用(通過保存按鈕的按下,測試和它激發井):

$.ajax({ 
url: "../../../../admin/includes/classes/class.article_front_Post.php",  
type: "POST", 
data: { 
     'articleid': $articleid, 
     'contenu': $contenu, 
     'name': $name 
     } 

}); 

這裏是...... front_Post.php文件的內容:

include_once('../../../../init.php'); 

$articleid = $_GET['articleid']; 
$contenu = $_GET['contenu']; 
$name = $_GET['name']; 

// $name = 'special1';    
// $contenu = '<p>test</p>'; 
// $articleid = '17'; 
// above to test the update (it works) 

mysql_query(" 

UPDATE al_articles SET $name='$contenu' 
WHERE (ArticleID='$articleid') 

") or die(mysql_error()); 
+0

什麼是錯誤? –

+0

沒有錯誤 - 感謝它現在解決了。見下文。 – Sergelie

+0

請注意:'mysql_ *'函數已被棄用,並已從PHP 7中刪除,已被'myslqi_ *'和'PDO'函數取代,這些函數在使用預準備語句和綁定參數時更安全。 –

回答

0

你的AJAX是使用 「POST」:

$.ajax({ 
url: "../../../../admin/includes/classes/class.article_front_Post.php",  
type: "POST", //<====================================================!!!! 
data: { 
     'articleid': $articleid, 
     'contenu': $contenu, 
     'name': $name 
     } 
}); 

但你的PHP是用 「GET」,因此,更換 「GET」 通過 「POST」:

include_once('../../../../init.php'); 

$articleid = $_POST['articleid']; //<=============================== 
$contenu = $_POST['contenu'];  //<=============================== 
$name = $_POST['name'];   //<=============================== 

// $name = 'special1';    
// $contenu = '<p>test</p>'; 
// $articleid = '17'; 
// above to test the update (it works) 

mysql_query(" 

UPDATE al_articles SET $name='$contenu' 
WHERE (ArticleID='$articleid') 

") or die(mysql_error()); 

或者在您的ajax中用「GET」取代「POST」。無論如何,GET參數可以在URL上看到,所以我建議POST。

+0

是的!謝謝!現在工作! (我會做我的作業,以瞭解郵政和獲取的東西! – Sergelie

+0

@ user3371049,你可以點擊複選標記接受答案^ __ –

+1

剛剛做了,再次感謝! - 有一個時間,防止過早做。 – Sergelie