0

Chrome console error: POST http://******************** 500 (Internal Server Error)

JS

$.ajax({
                    method: "POST",
                    url:"ajax.php",
                    data: {
                        x: x,
                        y: y
                    },
                    success:function(data){...}
                 });

PHP

<?php

date_default_timezone_set('Europe/Madrid');

$link = mysql_connect("*******", "*******", "******");
mysql_select_db("******") or die ("no database");


$name = $_POST['x'];
$score = $_POST['y'];

// Get the current date
$current_date = date('d-m-Y H:i:s');

$sqlCommand = "INSERT INTO hungry_bird (date_played, name, score)
               VALUES('$current_date ', '$name', '$score')";

$results = mysql_query($sqlCommand) or die (mysql_error());

mysql_close($link);


?>

Any idea what could be the problem? Thanks.

Abel Abad
  • 147
  • 1
  • 2
  • 13
  • Check the console and see what it says, right click inspect element console in chrome. – wuno Dec 26 '15 at 17:51
  • A 500 error in PHP indicates that you need to check your web server's error log for full details. Better still, _always_ when developing and testing code, add to the top of your highest included file `error_reporting(E_ALL); ini_set('display_errors', 1);` The error will come through the ajax response. – Michael Berkowski Dec 26 '15 at 17:51
  • I see you are using d-m-Y. If that is a MySQL DATETIME column (as it should be), the correct insert format is `Y-m-d H:i:s`, but that would not cause a 500 error. The code itself is also vulnerable to SQL injection in `$name,score`. Please read through [How can I prevent SQL injection in PHP](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) At a minimum, you must call `mysql_real_escape_string()` on those two variables, but if you are in any position to change the approach, now is the time to switch to `prepare()/execute()` with PDO (detailed in the link) – Michael Berkowski Dec 26 '15 at 17:52
  • 1
    ... because the `mysql_*()` functions were deprecated in PHP 5.5. – Michael Berkowski Dec 26 '15 at 17:54
  • open your console and tap on network tab you will find the request there there will be your php error – Alaa M. Jaddou Dec 26 '15 at 18:17

1 Answers1

1
$link = mysqli_connect("*******", "*******", "******");
mysqli_select_db($link, "******") or die ("no database");
$name = $_POST['x'];
$score = $_POST['y'];
$sqlCommand = "INSERT INTO hungry_bird (date_played, name, score)
               VALUES('CURDATE()', '$name', '$score')";
$results = mysqli_query($link, $sqlCommand) or die (mysqli_error($link));
mysqli_close($link);
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30