0

I have some issue with the insertion of data from an AJAX POST Request into mysql data base.

When I look into the POST Network of my console of my AJAX request, I have:

  • question1 item2

  • question2 ["lion","cat"]

So my ajax request works. But I cannot insert those two elements into my SQL data base. And the echo("ok") is not displayed as well. It seems like nothing has been post. Could you please help me? Thank you.

Here is my code:

<?php
require 'inc/functions.php';
require_once 'inc/db.php';
logged_only();

if(!empty($_POST)){

        echo("ok");
        $user_username = $_SESSION['auth']->username;
        $req = $pdo->prepare("INSERT INTO survey1 SET username = ?, question1=?, question2=?");
        $req->execute(array($user_username),$_POST['question1'],$_POST['question2']); 
}
?>
Thomas
  • 13
  • 5
  • Have you watched the AJAX request / response in the browser's developer tools? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Jun 19 '17 at 18:53
  • Are you trying to insert an array? `question2 ["lion","cat"]` – Hail Hydra Jun 19 '17 at 18:53
  • Can we see your AJAX code? – Jay Blanchard Jun 19 '17 at 19:08
  • yes I am trying to insert an array. Is it possible. I am running this on a local server (i.e. mySQL). My Ajax request/response is "Warning: PDOStatement::execute() expects at most 1 parameter, 3 given" , but when I do echo($_POST['question1']); and echo($_POST['question2']); at the end of my code. Answers are displayed in my Ajax request/response just after the error message mentioned above. – Thomas Jun 19 '17 at 19:18
  • Thank you guys I have found the mistake. I do not why but it should be execute(array($user_username, $_POST['question1'], $_POST['question2'])) instead of $req->execute(array($user_username),$_POST['question1'],$_POST['question2']); Thanks again – Thomas Jun 19 '17 at 19:36

0 Answers0