0

AJAX code

   $.ajax({
            type: "POST",
            url: "/javascript/script.php",
            data: {
                "name": the_name,
                "email": the_email
            },
            success: function(data) {
                console.log(data)
                var delay = 8000;
                setTimeout(function() {
PHP Code i have tried so many ways but it doesn't deliever even with console bug i dont find issues

< ?php
   include_once('javascript/db.php');
  
  $email = $_POST['name'];
  $password = $_POST['email'];
  
  if(mysql_query("INSERT INTO `user`(`id`, `name`, `email`) VALUES ('$name', '$email')"))
    echo "Successfully Inserted";
  else
    echo "Insertion Failed";
?>
William Juden
  • 35
  • 1
  • 1
  • 7
  • Stop using the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Apr 18 '17 at 18:07
  • Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 18 '17 at 18:07
  • @u_mulder could that be the reason? because i removed one field and still didn't work.. – William Juden Apr 18 '17 at 18:08
  • No, definitely. Mysql finds third value somewhere else, no reason to worry. – u_mulder Apr 18 '17 at 18:09
  • @SterlingArcher i dont mind the SQL injection attacks i just want it to send first before i reedit them – William Juden Apr 18 '17 at 18:09
  • @AlexHowanskyi dont mind the SQL injection attacks i just want it to send first before i reedit them – William Juden Apr 18 '17 at 18:10
  • Are you certain that you invoke the AJAX function from jQuery, do you have a log statement in front of the AJAX function invocation? You can log whatever you want in the AJAX callback, if the AJAX function doesn't even get invoked (in case of a misspelled event name or something) than the log obviously won't be printed out in the console. – Julian Apr 18 '17 at 18:10
  • Inspect the actual request in browser dev tools network for starters to see what status is – charlietfl Apr 18 '17 at 18:10
  • You're setting the variables `$email` and `$password`, then inserting the varables `$name` and `$email`. – Alex Howansky Apr 18 '17 at 18:12
  • 1
    William the issue here is this question isn't supposed to just help you, it's supposed to help those who visit it. So even though security isn't in mind for you, it makes no sense for us to teach bad, deprecated practices. – Sterling Archer Apr 18 '17 at 18:14
  • your ajax code is incomplete and we don't know from where `the_name,the_email` variable come from? – Alive to die - Anant Apr 18 '17 at 18:18
  • Is your id column set to auto increment? Your set-up matters for us being able to answer the question. – Vbudo Apr 18 '17 at 18:43
  • 1
    It was actually indicated in the comments that query `INSERT INTO user(id, name, email) VALUES ('$name', '$email')` is incorrect. If you only ever want the values for `name` and `email`, you shouldn't be specifying `id` in the columns list. Not only is this code vulnerable to [SQL Injection Attacks](http://stackoverflow.com/q/60174/2298301), you also run the risk of it failing totally if deployed with PHP 7. Besides, please check `NETWORK` tab in the console when dealing with AJAX and / or you could run the PHP script directly from address bar to verify if there are any errors in it. – Dhruv Saxena Apr 18 '17 at 19:24
  • @Vbudo yes its auto increment. – William Juden Apr 18 '17 at 21:01

0 Answers0