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.
asked Dec 26, 2015 at 17:46
Abel Abad
1451 gold badge2 silver badges13 bronze badges
1 Answer 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);
answered Dec 26, 2015 at 18:12
Alaa M. Jaddou
1,1891 gold badge11 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
error_reporting(E_ALL); ini_set('display_errors', 1);The error will come through the ajax response.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 At a minimum, you must callmysql_real_escape_string()on those two variables, but if you are in any position to change the approach, now is the time to switch toprepare()/execute()with PDO (detailed in the link)mysql_*()functions were deprecated in PHP 5.5.