gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:46:16 26 4
gpt4 key购买 nike

仅当一行尚不存在时,我才尝试创建一行。我正在尝试检查是否存在具有相同 steamid 的行,如果存在,则不执行任何操作。否则创建行。

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

<?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();
?>

最佳答案

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

<?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();
?>

希望这对您有帮助。 :)

关于PHP:如果行已存在,则不创建行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com