user3371049 user3371049 - 6 months ago 15
PHP Question

Issue with posting via Ajax to MySql

The Data base is not updated although I am pretty sure my code is good (did several tests). So there must be an issue (async, etc.). I need help to figure out what is wrong.

Here is my Ajax call (by the press of a Save button, it is tested and it fires well):

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

});


And here is the content of the ... front_Post.php file:

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());

Answer

Your ajax is using "POST" :

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

But your PHP is using "GET", so, replace the "GET" by "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());

Or replace the "POST" by "GET" in your ajax. Anyway, GET parameters can be seen on the URL, so I recommend POST.