PHP:如果已存在,则不要创建行

时间:2016-01-19 17:49:37

标签: php mysql

我正在尝试仅在尚未存在的情况下创建行。我正在尝试检查是否存在具有相同public static void main( String[] args ) { gg gg = new gg(); }的行,如果存在,则不执行任何操作。否则创建行。

每次刷新页面时,使用以下代码创建一行。

steamid

1 个答案:

答案 0 :(得分:1)

您忘记在数组变量中添加单引号。请尝试以下更新的代码:

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql    = "SELECT * FROM Main WHERE steamid = ".$steamprofile['steamid'];
//$sql = "SELECT setup FROM Main WHERE steamid = $steamprofile[steamid]";
$result = $conn->query($sql);
if(!isset($_SESSION['steamid'])) {

    steamlogin(); //login button

}  else {

    include ('../core-auth/userInfo.php'); //To access the $steamprofile array
        if ($result->num_rows > 0) {
            // output data of each row
                while($row = $result->fetch_assoc()) {
                    if (($row["setup"]) == 1){
                        echo "<br><div class='container'><div class='jumbotron'><div align='center'>";
                        echo "<h4>You have already Setup your account!</h4><br>";
                        echo "<a href='../index.php' class='btn btn-success btn-block' role='button'><span class='  glyphicon glyphicon-ok' aria-hidden='true'></span>  Back</a>";
                        exit;

        }

}  

    } else {
     $sql = "INSERT INTO Main (steamname, steamid, warns, notifi, setup)
VALUES ('".$steamprofile['personaname']."', '".$steamprofile['steamid']."', '0', '0', '1')";
if ($conn->query($sql) === TRUE) {
    echo "Added user account.";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}   
    }
    }

$conn->close();
?>

希望这会对你有所帮助。 :)