0

我有一个将数据插入 mySQL 的表单。它可以工作,但我正在尝试添加错误处理以防万一。如果我打破插入语句 mySQL 死了,但我仍然在前端收到成功消息。我究竟做错了什么?

AJAX

  function postData(){
  var employeeName = jQuery('#employeeName').val();
  var hireDate = jQuery('#hireDate').val();
  var position = jQuery('#position').val();
  var location = jQuery('#location').val();
  var interveiwer = jQuery('#interviewersID').val();
  var q01 = jQuery('#q01').val();
  var q02 = jQuery('#q02').val();
  var q03 = jQuery('#q03').val();
  var q04 = jQuery('#q04').val();
  var q05 = jQuery('#q05').val();
  var summary = jQuery('#summary').val();

  jQuery.ajax({
      type: 'POST',
      url: 'queryDay.php',
      data: 'employeeName='+ employeeName +'&hireDate='+ hireDate +'&position='+ position +'&location='+ location +'&interveiwer='+ interveiwer +'&q01='+ q01 +'&q02='+ q02 +'&q03='+ q03 +'&q04='+ q04 +'&q05='+ q05 +'&summary='+ summary,
      success: function(){
          jQuery('#formSubmitted').show();
      },
      error: function(jqXHR, textStatus, errorThrown){
          jQuery('#returnError').html(errorThrown);
          jQuery('#formError').show();
      }
  });
};

PHP

require_once 'config.php';

$employeeName = $_POST['employeeName'];
$hireDate = $_POST['hireDate'];
$position = $_POST['position'];
$location = $_POST['location'];
$interviewerID = $_POST['interveiwer'];
$q01 = $_POST['q01'];
$q02 = $_POST['q02'];
$q03 = $_POST['q03'];
$q04 = $_POST['q04'];
$q05 = $_POST['q05'];
$summary = $_POST['summary'];

mysql_query("INSERT INTO employee (name, hiredate, position, location) VALUES ('$employeeName', '$hireDate', '$position', '$location')") or die (mysql_error());

$employeeID = mysql_insert_id();

mysql_query("INSERT INTO day (employee, interviewer, datetaken, q01, q02, q03, q04, q05, summary) VALUES ('$employeeID', '$interviewerID', NOW(), '$q01', '$q02', '$q03', '$q04', '$q05', '$summary')") or die (mysql_error());
4

1 回答 1

1

问题出在你的头上

mysql 错误页面返回 200 OK 标头。您需要将其更改为错误标头,jquery 会将其视为错误,否则它将认为这是成功的。

或者另一方面,您可以捕获 mysql 错误并返回一些文本,然后您可以捕获并读取客户端,但这将在成功处理程序中捕获。

于 2012-10-17T23:09:59.427 回答