0

I am trying to write some data to a MySQL Table however the .post call is returning with a 500 server error. Any help in the right direction would be great.

I think it's something to do with the _POST variables not sending right.

Here is the code:

JS:

function write_table(response) {
  var data = {
    'user_id' : response.id,
    'user_email' : response.email,
    'user_first' : response.first_name,
    'user_last' : response.last_name
  };

  console.log(data);

  $.ajax({
    'url': './includes/php/login_facebook.php',
    'data': data,
    'type': 'POST',
    'beforeSend': function(xhr, settings) {
      console.log('ABOUT TO SEND');
    },
    'success': function(result, status_code, xhr) {
      console.log('SUCCESS!');
    },
    'complete': function(xhr, text_status) {
      console.log('Done.');
    },
    'error': function(xhr, text_status, error_thrown) {
      console.log('ERROR!', text_status, error_thrown);
    }
  });
}

PHP:

    <?php

ini_set('display_errors',1);
error_reporting(E_ALL);

$host = 'localhost';
$un = 'root';
$pw = 'root';
$db = 'bikelouis';

$user_id = $_POST['user_id'];
$user_email = $_POST['user_email'];
$user_first = $_POST['user_first'];
$user_last = $_POST['user_last'];

$conn = mysql_connect($host, $un, $pw) or die(mysql_error());

if ($conn) {
    echo '<script> alert("connected!");</script>';
    mysql_select_db($db) or die(mysql_error());
    $sql = "INSERT INTO users (user_id, user_email, user_first, user_last) VALUES ($user_id, $user_email, $user_first, $user_last)";

} else {
    echo 'Connection failed.';
}
?>

I am using facebook connect, that is where 'response' is coming from. That works perfectly.

4

1 回答 1

3

jQuery 端

$.post() 只是 $.ajax() 的一个包装器,所以如果你想对正在发生的事情有更多的控制和可见性,我强烈建议使用 $.ajax() 代替。

$.post()的data参数应该是键/值对的对象,而不是列表。也就是说,我不确定将data对象放入user_info列表中会完成什么,这可能是您问题的根源。

试试这个,让我知道它对你有用:

function write_table(response) {
  var data = { // This is the format $.post() expects.
    'user_email' : response.email,
    'user_id' : response.id,
    'user_first' : response.first_name,
    'user_last' : response.last_name
  };
  console.log(data);

  $.post('./includes/php/login_facebook.php', data, function(result, status, xhr) {
    console.log(status, result);
  });
}

相同的请求,通过$.ajax()

function write_table(response) {
  var data = {
    'user_email' : response.email,
    'user_id' : response.id,
    'user_first' : response.first_name,
    'user_last' : response.last_name
  };

  $.ajax({
    'url': './includes/php/login_facebook.php',
    'data': data,
    'type': 'POST',
    'beforeSend': function(xhr, settings) {
      console.log('ABOUT TO SEND');
    },
    'success': function(result, status_code, xhr) {
      console.log('SUCCESS!', 
    },
    'complete': function(xhr, text_status) {
      console.log('Done.');
    },
    'error': function(xhr, text_status, error_thrown) {
      console.log('ERROR!', text_status, error_thrown);
    }
  });
}

PHP端

首先,我强烈建议使用<?php而不是打开 PHP <?,因为并非在所有设置上都启用了后者。

其次,实际上在浏览器中显示错误不是接收内部服务器错误,而是更清晰。在您希望显示潜在错误的任何 PHP 脚本的开头,包括以下内容:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

至于您收到的 500 Internal Server Error,很可能是因为您在第 8 行$前面缺少了一个。$_POST

Instead of:
$user_id = _POST['user_id'];
It should read:
$user_id = $_POST['user_id'];

插入

  • 所有变量都应封装在刻度/撇号中。
  • 转义值也是一个好主意,以防止 SQL 注入攻击:

尝试这个:

$sql = "
  INSERT INTO users
  (user_id, user_email, user_first, user_last) VALUES (
    '" . mysql_real_escape_string($user_id) . "',
    '" . mysql_real_escape_string($user_email) . "',
    '" . mysql_real_escape_string($user_first) . "',
    '" . mysql_real_escape_string($user_last) . "'
  )
";
于 2013-06-19T00:38:37.847 回答