dongrong9938 2015-12-20 12:38
浏览 65
已采纳

PHP从数据库中选择结果,检查它们,进行更新并打印它们

I make you a semplified example of my problem and then try to explain it:

    <?php
$connect = mysqli_connect($link, $user, $pw, $db);

$informations = mysqli_fetch_array(mysqli_query($connect, "SELECT * FROM table WHERE id = '$id' "));

if($_POST['submit']){
    if($informations['show'] == 'true'){
        if($informations['changes'] <= '100'){
            $value = mysqli_real_escape_string($connect, $_POST['value']);
            mysqli_query($connect, "UPDATE table SET changes=changes+1, value='$value' WHERE id = '$id'");
        }else{
            echo "You made too much changes.";
        }
    }else{
        echo "You cannot change this.";
    }
}
if($informations['show'] == 'true'){
    echo "<form action='' method='post'>";
    echo "<input type='text' name='value' /><input type='submit' name='submit' value='Update' />";
    echo "</form>";
}
echo "<br/>You currently have made ".$informations['changes']." changes";
?>

So, looking at this code I can see this:
I enter my page and have a text input near a submit button
Under this i have written "You currently have made 0 changes."



If I press the submit button my page refresh and I can see:
A text input near a submit button
Under this i have written "You currently have made 0 changes."



If i refresh the page this time I can see:
A text input near a submit button
Under this i have writte "You currently have made 1 changes."

So what i wanna ask you is:
Is there any way to perform the update and directly see the changes on the page after the normal form refresh?
I cannot put the "update query" above the "select query" because before make the update i need to check the values of the user....
I know I may use AJAX but it's seems weird that php cannot do that without AJAX, and that takes more time...
Any help will be appreciated, thank you!

  • 写回答

1条回答 默认 最新

  • douxianglu4370 2015-12-20 12:57
    关注

    The reason is not linked to using Ajax or not. You are increasing the changes value in the database but not in your PHP variable.

    So just before this line:

            mysqli_query($connect, "UPDATE table SET changes=changes+1, value='$value' WHERE id = '$id'");
    

    add the following:

            $informations['changes']++;
    

    Remarks

    Although you have escaped quotes in $value to avoid SQL injection, it is better to prepare your statements instead of concatenating values into it.

    The field changes is numerical so this comparison is wrong:

    if($informations['changes'] <= '100'){
    

    It should be without quotes:

    if($informations['changes'] <= 100){
    

    Change the message "too much changes" to "too many changes", because changes is a plural.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败