【问题标题】:How to search for a specific column data using a variable and update its column data [closed]如何使用变量搜索特定列数据并更新其列数据 [关闭]
【发布时间】:2021-01-22 09:39:24
【问题描述】:

我正在尝试做的事情:我正在尝试更改“用户名”行下的特定列,其中用户名与 $loginuser var 相同,并将该列中的 speedrunhighscore 行更改为新的speedruhighscore.

问题: 在下面的代码中有一行我用粗体表示,这就是我试图运行以更改我的数据库中的数据但我的数据库中没有任何变化但回声都运行顺利的行。

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "smolgames";

    $speedrunhighscore = $_POST["speedrunhighscore"];
    $loginuser = $_POST["loginuser"];

    $conn = new mysqli($servername, $username, $password, $dbname);

    if($conn->connect_error){
        die("connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT username FROM userinfos WHERE username = '" . $loginuser . "'"; 
    $result = $conn->query($sql);

    if($result->num_rows > 0){        
      **$sql3 = "UPDATE userinfos SET speedrunhighscore = (' . $speedrunhighscore . ') WHERE username = '" . $loginuser . "'";**
      echo "updating your new highscore":

                if($conn->query($sql3) === TRUE){
                    echo "your highscore have been updated successfully!";

                }
                else{
                    echo "Error: ". $sql3 . "<br>" . $conn->error; 
                }
    } 
    else{
        echo "no usernames found";

        if($conn->query($sql2) === TRUE){
            echo "new highscore send successfully";
        }
        else{
            echo "Error: ". $sql2 . "<br>" . $conn->error; 
        }
    }   

    $conn->close();
?>

注意:变量 loginuser 从我使用 unity C# 发布的字符串更改

【问题讨论】:

    标签: php mysql sql mysqli


    【解决方案1】:

    对于初学者,您应该使用带有有界占位符的准备好的语句。这可以确保您的查询不会受到 SQL 注入攻击,并确保即使是 O'Riley 这样的用户名也可以正常工作。

    接下来,您无需在更新之前检查该行是否存在 - 您可以尝试立即执行更新,并检查实际上更新了多少行。

    最后,您应该将 MySQLi 连接配置为在出错时抛出异常,这意味着您不必为每个查询进行单独的错误处理。

    <?php
    // Configure MySQLi to throw exceptions on failure instead 
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "smolgames";
    
    $speedrunhighscore = $_POST["speedrunhighscore"];
    $loginuser = $_POST["loginuser"];
    
    try {
        $conn = new mysqli($servername, $username, $password, $dbname);
    
        $stmt = $conn->prepare("UPDATE userinfos
                                    SET speedrunhighscore = ?
                                    WHERE username = ?");
        $stmt->bind_param("ss", $speedrunhighscore, $loginuser);
        $stmt->execute();
        $affectedRows = $stmt->affected_rows;
        $stmt->close();
        
        if ($affectedRows) {
            echo "your highscore have been updated successfully!";
        } else {
            echo "no usernames found";
        }
    } catch (Exception $e) {
        // Handle the exception 
        // Log it, send a message to the user "something went wrong"
    }
    

    您应该实现某种身份验证和授权层,因为现在您可以提交具有任意高分的其他用户名,并且您基本上可以更新表格中的任何分数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多