LedZelkin
LedZelkin

Reputation: 586

Insert into my database with jQuery (ajax) and PHP

i'm trying to insert some data in my table: golden-book. All is good , i dont have any error , the answers are good but there isn't no data insert in my table.

i don't know what is wrong with this code , maybe you can help me ?

my ajax request in golden-book.js

function insert_messages(auteur_message,message){
$.ajax({    

    type : "POST",
    cache: false,  
    url : "insert-messages.php",
    data:{
        auteur_message:auteur_message,
        message:message
    },
    success: function() {   
    },
    error : function() {//en cas de problème de requete AJAX
        alert("Sorry, The requested property could not be found.");//affichage d'un mesage d'erreur
    }
});
} 

insert-messages.php

<?php

$auteur_message = $_POST['auteur_message'];
$message = $_POST['message'];
try {
// On se connecte à MySQL
$bdd = new PDO('mysql:host=localhost;dbname=photo', 'root', '');
} catch (Exception $e) {
// En cas d'erreur, on affiche un message et on arrête tout
die('Erreur : ' . $e->getMessage());
}

$req = $bdd->prepare('INSERT INTO photo.golden_book (auteur-message , message) VALUES (:auteur , :message)'); 
$req->execute(array(
'auteur' => $auteur_message,
'message' => $message));
?>

Thank you all in advance for taking your time to help me.

Upvotes: 4

Views: 2575

Answers (1)

Marc B
Marc B

Reputation: 360912

auteur-message is an invalid field name. MySQL will interpret it as auteur MINUS message. See: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html. Doing math on fields which don't exist yet also makes no sense - can't subtract things that haven't been inserted yet.

You can try quoting it with backticks:

... .gold_book(`auteur-message`, ...

but really, you should rename the field. Escaping is a hack.

Upvotes: 4

Related Questions