Paste into my database using jQuery (ajax) and PHP

I am trying to insert some data into my table: gold-book. Everything is fine, I have no errors, the answers are good, but there is no data 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 for taking the time to help me.

+4
source share
1 answer

auteur-message is an invalid field name. MySQL will interpret it as an auteur MINUS message . See: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html . Performing math in fields that do not yet exist also makes no sense - it cannot subtract things that have not yet been inserted.

You can try quoting it with backlinks:

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

but in fact you should rename this field. Escaping is a hack.

+4
source

All Articles