多个MySql插入&更新未按预期运行

时间:2014-10-01 00:03:59

标签: php mysql sql database mysqli

我正在尝试将信息插入到表中,并在一个字符串中更新另一个表(在同一个数据库中)中的两行。我的印象是,您可以通过在每次尝试后添加分号来完成此操作,并且它将在相同的字符串中单独操作它们。

我想不出任何其他方法可以实现在“游戏”表中插入数据并同时更新“播放器”表中两个唯一行中的数据所需的效果。

这是我的代码......

<?php

include_once('database-details.php');

$winner         = mysqli_real_escape_string($db, $_REQUEST['winner']);
$loser              = mysqli_real_escape_string($db, $_REQUEST['loser']);
$balled         = mysqli_real_escape_string($db, $_REQUEST['balled']);
$fixtures           = 'games';
$table              = 'players';

$sql = "INSERT INTO `$fixtures` "
    . "(`id`, `winner`, `loser`, `balled`) VALUES "
    . "(NULL, '$winner', '$loser', '$balled') ;"
    . "UPDATE `$table` SET apps = apps + 1, wins = wins + 1, balled = balled + $balled WHERE id = $winner ;"
    . "UPDATE `$table` SET apps = apps + 1, losses = losses + 1, balled = balled - $balled WHERE id = $loser";

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

$response = (object)array('status' => 'error');

if($result){
    $response = (object)array(
        'status'    => 'ok',
        'id'        => $db->insert_id,
    );
}

header('Content-type: application/json');
echo json_encode($response);

?>

...脚注 表单随ajax一起提交,然后更新前端的内容。我可以确认,当我只有一个请求测试时,提交工作正常,所有三个请求都是单独提交的,并且每个工作都正常。它只会在我合并时才会中断。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

它看起来像一个并发问题。您可能希望使用将autocommit设置为off,并使用mysqli_begin_transaction()设置块,执行查询和mysqli_commit()来处理块。

类似的东西:

    mysqli_autocommit($db, FALSE);
    mysqli_begin_transaction();
    //now call query() as many times as needed
    mysqli_commit(); 

答案 1 :(得分:0)

您是否必须在同一查询中执行更新查询?

也没有意义

$response = (object)array('status' => 'error');

if($result){

因为您已经停止了

上面一行的脚本执行
if(!$result = $db->query($sql)){

也许试试:

<?php

include_once('database-details.php');

$winner         = mysqli_real_escape_string($db, $_REQUEST['winner']);
$loser              = mysqli_real_escape_string($db, $_REQUEST['loser']);
$balled         = mysqli_real_escape_string($db, $_REQUEST['balled']);
$fixtures           = 'games';
$table              = 'players';

$sql = "INSERT INTO `$fixtures` "
    . "(`id`, `winner`, `loser`, `balled`) VALUES "
    . "(NULL, '$winner', '$loser', '$balled') ;"

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
else{
    $insertid = $db->insert_id
    $sql = "UPDATE `$table` SET apps = apps + 1, wins = wins + 1, balled = balled + $balled WHERE id = $winner ;"
    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }
    $sql = "UPDATE `$table` SET apps = apps + 1, losses = losses + 1, balled = balled - $balled WHERE id = $loser";
    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }
    $response = (object)array(
        'status'    => 'ok',
        'id'        => $insertid ,
    );
}

header('Content-type: application/json');
echo json_encode($response);

?>

根据你的$ db类,如果在一个查询中运行多个sql语句,insert_id的值可能不是你所期望的。

答案 2 :(得分:0)

谢谢大家的快速回复。最后,链接@AR为mysqli_multi_query()提供了诀窍。以下是它现在的样子(如果感兴趣的话)......

<?php

include_once('database-details.php');

$winner    = mysqli_real_escape_string($db, $_REQUEST['winner']);
$loser     = mysqli_real_escape_string($db, $_REQUEST['loser']);
$balled    = mysqli_real_escape_string($db, $_REQUEST['balled']);
$fixtures  = 'games';
$table     = 'players';

$sql = "INSERT INTO `$fixtures` "
    . "(`id`, `winner`, `loser`, `balled`) VALUES "
    . "(NULL, '$winner', '$loser', '$balled') ;"
    . "UPDATE `$table` SET apps = apps + 1, wins = wins + 1, balled = balled + $balled WHERE id = $winner ;"
    . "UPDATE `$table` SET apps = apps + 1, losses = losses + 1, balled = balled + $balled WHERE id = $loser";

if (!$db->multi_query($sql)) {
    echo "Multi query failed: (" . $db->errno . ") " . $db->error;
}

do {
    if ($result = $db->store_result()) {
        $response = (object)array(
            'status'    => 'ok'
        );
        $result->free();
    }
} while ($db->more_results() && $db->next_result());

header('Content-type: application/json');
echo json_encode($response);

?>    

PS - 感谢@Hadyn Dickson提供有关查询的建议。