我想知道我的代码有什么问题。我想要做的是将数据输入INTO我的数据库但是在插入之前,检查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提交的任何验证...有人能告诉我这有什么问题吗?有人可以帮我解决吗?
答案 0 :(得分:1)
您获得现有的行$row = $stmt->fetch(PDO::FETCH_ASSOC);
,但您没有做任何事情。使用if ($stmt->rowCount() == 0)
检查行数是否为零,然后执行其余代码并插入数据。
答案 1 :(得分:0)
如果你需要从服务器端显示ajax响应。你只需回应服务器端的任何内容。你会在ajax成功alert(re);
中得到这样的
<强>的Ajax:强>
success:function(re)
{
alert(re); //here you get the message from server side
window.location = "addEvents.php";
}
<强> PHP:强>
在php中,您必须添加if if condition来检查数据是否重复,或者不是这样的
<?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";
}
}
}
?>
答案 2 :(得分:0)
单独页面上的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
}
?>