2016-05-23 73 views
1

尝试使用InnoDB表插入数据到MySQL数据库时遇到了问题。问题如下:当gamescreen.php得到执行无论如何变成第一个插入行后,另一个插入从questions_de另一个随机选取的行,其中$id0其中第一个有5。为什么会变成另一个?插入查询变成执行一次的两倍

表questions_de:

ID INT

问题VARCHAR

answer_m浮动

$.ajax({ url: 'gamescreen.php', 
data: {id: '5'}, 
type: 'GET', 
cache: false, 
async: false, 
success: function() { 
     window.location.href='gamescreen.php';  
      } 
}); 

gamescreen.php

if (isset($_GET['id'])) { 
$id = $_GET['id']; 
echo $id; 
} 
    $new = 0; 
     try { 
           $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socgame",$user,$password); 

           $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line 
           $sql = "SELECT * 
     FROM user WHERE id = '$id' 
     LIMIT 1"; // 
      if ($res = $dbh->query($sql)) {// need to add this line in your code 
       // then after fetchColumn 
      $user2name = $res->fetchAll(); 

      } 

      if($user2name > 0) { 
       //do something 
      } 
      else { 

       echo "Sorry something happen wrong with our servers."; 
      } 
     } 
     catch(PDOException $e) { 


     } 

     try { 
           $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socgame",$user,$password); 

           $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line 
           $sql = "SELECT * 
     FROM questions_de 
     ORDER BY RAND() 
     LIMIT 1"; // 
      if ($res = $dbh->query($sql)) {// need to add this line in your code 
       // then after fetchColumn 
      $question = $res->fetchAll(); 

      } 

      if($question > 0) { 
       //do something 
      } 
      else { 

       echo "Sorry something happen wrong with our servers."; 
      } 
     } 

     catch(PDOException $e) { 


     } 


         try { 
           $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socgame",$user,$password); 

           $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line 
           $sql = "INSERT INTO game_create (user1, user2, user1name, user2name, question, questionid, answer) 
           VALUES ('".$_COOKIE["userid"]."', '".$id."', '".$_COOKIE["username"]."', '".$user2name[0]["username"]."', '".$question[0]['question']."', '".$question[0]['id']."', '".$question[0]['answer_m']."')"; 

           if ($dbh->query($sql)) { 
             //echo "New Record Inserted Successfully"; 
           } 
           else{ 
             // echo "Data not successfully Inserted."; 
           } 
           $new = $dbh->lastInsertId(); 
           $dbh = null; 
         } 
         catch(PDOException $e) 
         { 
           echo $e->getMessage(); 
         } 

         if ($new > 0) { 

         } else { 
         echo 'Sorry something went wrong.';  
         } 
+3

可能是第二次由于这个'window.location.href ='gamescreen.php'; ' – Saty

+0

您是否检查过您的JavaScript是否执行过两次? –

+0

@Saty为什么这是错误,我该如何解决? – Maxxopu

回答

0

问题看起来与这条线:

成功:函数(){window.location.href = 'gamescreen.php';}

当你的AJAX成功运行其重定向到gamescreen.php反过来触发PHP代码再次给你两个代码运行

要么简单地删除AJAX和直接链接到gamescreen.php,如果这就是你想要结束的页面,而是创建一个新的页面将用户重定向到成功 - 例如,success.php

+0

哦,人感谢你我试图解决它36小时:D。我现在用一个链接按钮。 – Maxxopu