-1

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
5
  • Check the console and see what it says, right click inspect element console in chrome. Commented Dec 26, 2015 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. Commented Dec 26, 2015 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 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) Commented Dec 26, 2015 at 17:52
  • 1
    ... because the mysql_*() functions were deprecated in PHP 5.5. Commented Dec 26, 2015 at 17:54
  • open your console and tap on network tab you will find the request there there will be your php error Commented Dec 26, 2015 at 18:17

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.