0

我有一个button属性onclick来启动 javascipt 函数,并且我的 javascript ajax 脚本有xmlhttp对象。它正在与dbaction.php

当我单击按钮时,脚本和 mysql 一切正常,但问题在于行。It is inserting TWO same row for each click until it rowCount>0 当第一次单击 echo prints "0"forrowCount()后,如果我现在再次单击 echo prints "2"forrowCount()

问题出在哪里?

在此处输入图像描述

<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

<?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;
}
4

2 回答 2

1

检查差异:-

<?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;
}

在最后一个 else 块中,您同时使用 query() 和 execute(),用于直接执行 sql 查询一个 query(),execute() 用于准备好的语句

于 2013-06-25T18:39:08.240 回答
0

就像@dianuj 在他的评论中说的那样,如果你在 中同时看到两个请求,那么像下面 console这样更改你的 jsfunction

<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>
于 2013-06-25T18:49:44.083 回答