Returning mySQL error with jQuery & AJAX
- by kel
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());