0

I've got a form inserting data into mySQL. It works but I'm trying to add error handling in case something happens. If I break the Insert statements mySQL dies but I'm still getting a success message on the front end. What am I doing wrong?

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());
6
  • As an aside, you should use PDO instead, as the mysql_* functions are deprecated. I also hope this site is not live, as as it stands, your code is vulnerable to injection attacks. Commented Oct 17, 2012 at 23:02
  • Just wondering...why not use $('#some_id').foo() instead of jQuery('#some_id'.foo()? Commented Oct 17, 2012 at 23:08
  • @ecbrodie I don't see anywhere that they do that. Commented Oct 17, 2012 at 23:09
  • 1
    @ecbrodie are you talking about using jQuery instead of $? I've always done that to avoid issues when I'm working in Joomla or WordPress. Commented Oct 17, 2012 at 23:11
  • 1
    He may be using prototype in which case jQuery is okay, but something shorter is probably advisable. Commented Oct 17, 2012 at 23:11

1 Answer 1

1

The issue is with your head(ers)

The mysql error page is returning a 200 okay header. You will need to change this to an error header and jquery will pick it up as an error, else it will think it is a success.

Or on the other hand you could catch the mysql error and return some text that you then catch and read client side, but this will be caught within the success handler.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.