简体   繁体   English

数据不会保存到MySQL数据库中

[英]Data won't save into MySQL database

I can connect to my DB, but nothing saves into it. 我可以连接到数据库,但是没有任何保存。 In the section where it is suppose to save it into the DB, it echos "New User" and the $sql line with the data that should be saved. 在假定将其保存到数据库的部分中,它将回显“ New User”和$ sql行以及应保存的数据。 Can anyone see why this shouldn't be saving my data? 谁能看到为什么这不应该保存我的数据?

$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
//$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if($dbh){
echo "Connected successfully";
}else{
    die("Connection failed: " . mysqli_connect_error());
}
if(isset($_SESSION['steamid'])) {
include ('steamauth/userInfo.php');
        if (!empty($steamprofile['steamid'])) {
             $stmt = $dbh->prepare("SELECT count(*) from user WHERE steam_id = :steam_id");
            $stmt->bindValue(':steam_id', $steamprofile['steamid']);
            $stmt->execute();
            $count = $stmt->fetchColumn();
        }
//Row will return false if there was no value
        if ($count == 0) {
            //insert new data
            echo "New user";
            $sql = "INSERT INTO user (display_name, user_url, steam_id, profile_image)
    VALUES ('$steamprofile[personaname]', '$steamprofile[profileurl]', $steamprofile[steamid], '$steamprofile[avatar]')";
            echo($sql);
        //  die();
        } else {
            //User exist
            echo "User exists";
        }

}else{
echo "no user signed in";
}

Table Schema: http://gyazo.com/ef9badc3ae72b73557ed80efe2413ea3 表格架构: http//gyazo.com/ef9badc3ae72b73557ed80efe2413ea3

You didn't execute the INSERT sql statement. 您没有执行INSERT sql语句。 You can use the following statement after $sql : 您可以在$sql之后使用以下语句:

$result = mysqli_query($sql);

Make sure you read the $result and do appropriate things, eg: 确保您阅读了$result并做了适当的事情,例如:

if($result === true) {
   // success
} else {
   // failed
}

There it goes. 到了。

if ($count == 0) {
            echo "New user";
            $sql = "INSERT INTO user (display_name, user_url, steam_id, profile_image)
    VALUES ('$steamprofile[personaname]', '$steamprofile[profileurl]', $steamprofile[steamid], '$steamprofile[avatar]')";
            $dbh->query($sql); // You missed that line of code.
            echo($sql); // This will only echo out your query, not the result.
        } else {
            //User exist
            echo "User exists";
        }

As in your codes the $sql has not been executed, it will print only the variable. 由于在您的代码中$ sql尚未执行,因此它将仅输出变量。 Execute it first. 首先执行它。

Execute insert query. 执行插入查询。 Try this snippet in your code. 在您的代码中尝试使用此代码段。

 try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch (PDOException $ex) { } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM