[英]Check if the whole row exists
我想知道我的代碼出了什么問題。我想做的是在插入數據之前將數據發布到數據庫中,檢查是否存在EQUAL行,如果存在則顯示一條消息。
這是我在頁面頂部的查詢:
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');
}
?>
這是頁面底部的腳本:
<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>
但是沒有用,數據保存到數據庫中,並且沒有通過ajax提交的任何驗證...有人可以告訴我這是怎么回事嗎? 有人可以幫我解決嗎?
您將獲得現有行$row = $stmt->fetch(PDO::FETCH_ASSOC);
但是你什么也沒做。 使用if ($stmt->rowCount() == 0)
檢查行數是否為零,然后執行其余代碼並插入數據。
如果需要從服務器端顯示ajax響應。 您只是在服務器端回顯任何內容。 您將在ajax成功alert(re);
收到該消息alert(re);
像這樣
阿賈克斯:
success:function(re)
{
alert(re); //here you get the message from server side
window.location = "addEvents.php";
}
PHP:
在PHP中,您必須添加條件條件來檢查數據是否重復或不這樣
<?php
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);
if ($stmt->rowCount()==0)
{
$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');
echo "inserted succesfully ";
}
else
{
echo "not inserted because data duplicate";
}
}
}
?>
單獨頁面上的PHP應該看起來更像:
<?php
if(isset($_POST['submitAddEvents'])){
// obviously these other things are also already set unless there's a hack
$tA = trim($_POST['teamA']); $tB = trim($_POST['teamA']);
$eD = trim($_POST['eventDate']); $tE = trim($_POST['timeOfEvent']);
$lV = trim($_POST['live']);
// Verify if the data exists
if($ta !== '' && $tB !== '' && $eD !== '' && $tE !== '' && $lV !== ''){
// I like to save steps
$db->query("INSERT events (teamA, teamB, eventDate, `time`, live) VALUES ('$tA', '$tB', '$eD', '$tE', '$lV')");
}
// doesn't look like you need to query the information you just entered - code above assumes STRING like data in MySQL, which may not be the case since we can't see your database
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.