我想把PHP的条件放在MySQL的更新列上

时间:2018-01-24 14:00:22

标签: php mysql

我想放PHP条件如果更新了“Credit”列,那么也在MySQL Table上更新“PlayerPower”列。

并且这是我的代码,我提出一个条件,如果“Credit”列小于5然后给出错误:“您的信用额度不够,请购买信用”,如果是,则更新“信用”列。我的问题是更新“PlayerPower”条件列。请指导我怎么可能?

c

3 个答案:

答案 0 :(得分:1)

关于sql injection的评论,你可以不改变初始选择查询来测试大于5的信用 - 如果记录集为空,那么用户没有信用 - 否则更新。

$sql = "select `credit` from `".$dbtable."` 
            where `username` = '$wusername' and `credit` >= 5";
$result = $conn->query( $sql );

if ( $result->num_rows == 1 ) {

    $sql="update `".$dbtable."` set `credit` = `credit` - 5, `playerpower` = `playerpower` + 5 where `username` = '$wusername' ";

    $result = $conn->query( $sql );
    exit( header('Location: '.$success_page ) );
} else {
    echo "You do not have enough credit! Please buy credits";
}

答案 1 :(得分:0)

        if ($row['Credit'] < 5) {
            echo "Your Credit is not Enough , Please Buy a Credit";
        }    
        else {
           $sql2 = "UPDATE `".$dbtable."` SET `Credit` = `Credit` - 5, PlayerPower = 100 WHERE `username` = '$wusername' AND `Credit` >= 5";
           $conn->query($sql2);
        }

编辑1 无论您想检查什么 - 请检查。 Ше现在取决于你。由于您已经检查了credit < 5是否意味着您在UPDATE时永远不会调用此credit = 0查询,因为 0&lt; 5

答案 2 :(得分:0)

请试试这个:

$ sql2 =&#34; UPDATE $(document).ready(function() { $('.frequency-item').each(function(i, e) { children = $(e).children(); observationLengthInMinutesId = children[1].id; timerId = children[3].id; startTimer(observationLengthInMinutesId, timerId); }); }); function startTimer(observationLengthInMinutesId, timerId) { w = null; if (typeof(Worker) !== "undefined") { if (w == null) { w = new Worker("/js/simple-timer.js"); w.postMessage($('#' + observationLengthInMinutesId).val()); } w.onmessage = function(event) { $('#' + timerId).text(event.data); //the problem is here if (event.data == '00:00') { w.terminate(); } /* I fixed in the following way but without terminating the workers! I don't like it if($( '#' + timerId ).text() != '00:00') { $( '#' + timerId ).text(event.data); } */ }; } else { // Web workers are not supported by your browser $('#' + timerId).text("Sorry, your browser does not support Web Workers ..."); } } SET Credit = Credit - 5,PlayerPower = PlayerPower +5;

您可以避免冗余代码:WHERE ".$dbtable." =&#39; $ wusername&#39; AND username&gt; = 5&#34 ;;

对Ashkan的问题:请纠正我的理解。所以如果Credit是&gt; 5然后你想要通过&#39; Credit&#39;更新表中的Credit列。 - 5?和PowerPlayer by PowerPlayer + 5