[英]Check if the whole row exists
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: 我想知道我的代码出了什么问题。我想做的是在插入数据之前将数据发布到数据库中,检查是否存在EQUAL行,如果存在则显示一条消息。
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? 但是没有用,数据保存到数据库中,并且没有通过ajax提交的任何验证...有人可以告诉我这是怎么回事吗? Can someone help me to fix it?
有人可以帮我解决吗?
You get the existing row $row = $stmt->fetch(PDO::FETCH_ASSOC);
您将获得现有行
$row = $stmt->fetch(PDO::FETCH_ASSOC);
but you didn't do anything with it. 但是你什么也没做。 Use
if ($stmt->rowCount() == 0)
checking if row count is zero then execute the rest of the code and insert data. 使用
if ($stmt->rowCount() == 0)
检查行数是否为零,然后执行其余代码并插入数据。
if you need to show ajax response from server side. 如果需要从服务器端显示ajax响应。 you just echo anything in server side .
您只是在服务器端回显任何内容。 you will get that in ajax success
alert(re);
您将在ajax成功
alert(re);
收到该消息alert(re);
like this 像这样
Ajax: 阿贾克斯:
success:function(re)
{
alert(re); //here you get the message from server side
window.location = "addEvents.php";
}
PHP: PHP:
In php you have to add that if condition to check if data is duplicate or not like this 在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 on a separate page should look more like: 单独页面上的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.