[英]Ajax and Mysql Inserting 2 same row for one click
I have a button
which has attribute onclick
to start javascipt function and my javascript ajax script has xmlhttp
object. 我有一个具有属性
onclick
的button
,用于启动javascipt函数,而我的javascript ajax脚本具有xmlhttp
对象。 It is communicating with dbaction.php
它正在与
dbaction.php
通信
When I click button everything is okay about script and mysql but the problem is about rows. 当我单击按钮时,关于脚本和mysql的一切都很好,但是问题是关于行的。
It is inserting TWO same row for each click until it rowCount>0
and when after first click echo prints "0"
for rowCount()
, then if I click again now echo prints "2"
for rowCount()
It is inserting TWO same row for each click until it rowCount>0
并且在第一次单击后,echo为rowCount()
打印"0"
,然后如果我再次单击,现在echo为rowCount()
打印"2"
Where is the problem? 问题出在哪儿?
<body>
<script type="text/javascript">
function loadName() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
{
document.getElementById("sample_div").innerHTML = xmlhttp.responseText;
$("#my_button").attr("value", "Sent");
$("#my_button").attr("style", "color:red;");
}
};
xmlhttp.open("GET", "dbaction.php?sender_id=5&receiver_id=6", true);
xmlhttp.send();
}
</script>
<input type="submit" id="buton" onclick="loadName();" value="Submit"/>
<div id="sample_div" style="background-color: khaki; width:50%; margin: 0 auto;">
</div>
</body>
dbaction.php dbaction.php
<?php
//Database connection using PHP PDO(php database objects)
try {
$db = new PDO("mysql:dbname=member;host=localhost", "root", "", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
catch ( PDOException $e ) {
echo $e->getMessage();
}
$sql= "SELECT * FROM friendship WHERE sender_id=".$_GET["sender_id"]." AND receiver_id=".$_GET["sender_id"]."";
$rs=$db->prepare($sql);
$rs->execute();
$rs->fetch();
$count = $rs->rowCount();
if ($count>0){
echo "You already sent request...";
echo $count;
}
else {
$query = $db->query("INSERT INTO friendship (sender_id, receiver_id, is_approved) VALUES (".$_GET["sender_id"].",".$_GET["receiver_id"].",0)" );
$query->execute();
echo "Request sent";
echo $count;
}
check the difference :- 检查差异:-
<?php
//Database connection using PHP PDO(php database objects)
try {
$db = new PDO("mysql:dbname=member;host=localhost", "root", "", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
catch ( PDOException $e ) {
echo $e->getMessage();
}
$sql= "SELECT * FROM friendship WHERE sender_id=".$_GET["sender_id"]." AND receiver_id=".$_GET["sender_id"]."";
$rs=$db->prepare($sql);
$rs->execute();
$rs->fetch();
$count = $rs->rowCount();
if ($count>0){
echo "You already sent request...";
echo $count;
}
else {
$query = $db->query("INSERT INTO friendship (sender_id, receiver_id, is_approved) VALUES (".$_GET["sender_id"].",".$_GET["receiver_id"].",0)" );
//$query->execute();
echo "Request sent";
echo $count;
}
in last else block you are using query() and execute() both,used for execute sql queries one query() directly and execute() for prepared statement 在最后一个else块中,您同时使用query()和execute(),用于执行sql直接查询一个query()和execute()来准备语句
like @dianuj said in his comment, if you saw two requests at the same time in console
, then change your js function
like below 就像@dianuj在他的评论中说的那样,如果您在
console
同时看到两个请求,则如下所示更改js function
<script type="text/javascript">
var in_request = false;
function loadName() {
if ( in_request ) {
return false;
}
in_request = true;
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if ( xmlhttp.readyState === 4 && xmlhttp.status === 200 ) {
in_request = false;
document.getElementById( "sample_div" ).innerHTML = xmlhttp.responseText;
$("#my_button").attr( "value", "Sent" );
$("#my_button").attr( "style", "color:red;" );
}
};
xmlhttp.open( "GET", "dbaction.php?sender_id=5&receiver_id=6", true );
xmlhttp.send();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.