I would like to know what is wrong with my code.. What I'm trying to do is POST data INTO my database BUT BEFORE INSERT, check if an EQUAL row exists and if exists show an message.. I tried it:
Here's my querys at the top of the page:
if(isset($_POST['submitAddEvents'])){
// Verify if the data exists
if(!filter_var($_POST['teamA'] && $_POST['teamB'] && $_POST['eventDate'])){
$stmt = $db->prepare('SELECT * FROM events WHERE teamA = :teamA AND teamB = :teamB AND eventDate = :eventDate');
$stmt->execute(array(
':teamA' => $_POST['teamA'],
':teamB' => $_POST['teamB'],
':eventDate' => $_POST['eventDate'],
));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
$statement = $db->prepare("INSERT INTO events(teamA, teamB, eventDate, `time`, live) VALUES (:teamA, :teamB, :eventDate, :timeOfEvent, :live)");
$statement->execute(array(
':teamA' => $_POST['teamA'],
':teamB' => $_POST['teamB'],
':eventDate' => $_POST['date'],
':timeOfEvent' => $_POST['timeOfEvent'],
':live' => $_POST['live'],
));
$id = $db->lastInsertId('ID');
}
?>
Here's my script at the bottom of the page:
<script>
//Saving Data
$('#saveButton').click(function(){
var teamA = $('#teamA').val();
var teamB = $('#teamB').val();
var date = $('#date').val();
var timeOfEvent = $('#timeOfEvent').val();
var live = "0";
if($("#live").is(':checked'))
live = $('#live').val();
$.ajax({
url : "<?= $_SERVER['PHP_SELF'] ?>",
type : "POST",
async: false,
data :
{
'submitAddEvents' : 1,
'teamA' : teamA,
'teamB' : teamB,
'date' : date,
'timeOfEvent' : timeOfEvent,
'live' : live,
},
success:function(re)
{
window.location = "addEvents.php";
}
});
});
</script>
But didn't work, the data saves into database and doesn't pass for any verification on ajax submit... Could someone tell me what's wrong with that? Can someone help me to fix it?