我尝试使用InnoDB表将数据插入到mysql数据库时遇到了麻烦。问题如下:当gamescreen.php
执行后,无论如何变为第一个插入的行之后,另一个插入另一个随机选取的questions_de
行,其中$id
是0
有5
。为什么要创建另一个?
table questions_de:
id int
问题varchar
answer_m float
$.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.';
}
答案 0 :(得分:0)
问题看起来像这一行:
成功:function(){window.location.href =&#39; gamescreen.php&#39 ;;}
当您的AJAX成功运行时,它会重定向到gamescreen.php,而后者又会再次触发PHP代码,为您提供两次代码运行
要么简单地删除AJAX并直接链接到gamescreen.php,如果那是你想要结束的页面,或者改为创建一个新的页面来重定向用户成功 - 例如success.php