尝试使用InnoDB表插入数据到MySQL数据库时遇到了问题。问题如下:当gamescreen.php
得到执行无论如何变成第一个插入行后,另一个插入从questions_de
另一个随机选取的行,其中$id
是0
其中第一个有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.';
}
可能是第二次由于这个'window.location.href ='gamescreen.php'; ' – Saty
您是否检查过您的JavaScript是否执行过两次? –
@Saty为什么这是错误,我该如何解决? – Maxxopu