gpt4 book ai didi

php - 在插入之前检查值,如果已经存在,请不要在php中使用mysql插入它

转载 作者:行者123 更新时间:2023-11-29 15:14:41 28 4
gpt4 key购买 nike

我有代码检查ips地址(如果重复)将重定向到另一个链接,但是当第一次在表(ip,user_agent,time)中的(click)中插入值时,代码运行良好,但是当我尝试检查是否以某个值运行时它编写了再次在数据库上,我需要任何值(ip,user_agent,time)在数据库上写完之后只写一次,而没有写一些值(ip,user_agent,time

<?php
$servername = "localhost";
$username = "root";
$password = "rootroot";
$dbname = "data_clicks";
//Create connection to database using mysqli
$conn = new mysqli($servername, $username , $password, $dbname);



//Set variables according to user input on previous form
if(isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['HTTP_USER_AGENT'])){
$ip = $_SERVER['REMOTE_ADDR'];
$user = $_SERVER['HTTP_USER_AGENT'];
$datetime = date_create()->format('d-m-Y ');
}


//Define the INSERT statement with IGNORE keyword
$sql = "INSERT IGNORE INTO clicks (ip, user_agent, time )
VALUES ('.$ip.', '.$user.', CURRENT_TIMESTAMP)";
if ($conn->query($sql) === false) {
die("Database error:".$conn->error);
}
// link offer
$file = "md5.txt";
$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);
$link="https://facebook.com/?".$line;
//do smth

$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));

// Check for success
if ($conn->affected_rows == 0) {
print "<br>Error! <p></p>";
echo "" . $ip .''.$user. " already exists in the DCR";
header("Location: http://www.google.com/");
} else {

//Success and return new id
echo "<br><p></p>Record Added!<p></p>";
header("Location: $link");
}
?>

最佳答案

我解决了问题,现在脚本可以正常工作了。

<?php
$servername = "localhost";
$username = "root";
$password = "rootroot";
$dbname = "data_clicks";
//Create connection to database using mysqli
$conn = new mysqli($servername, $username , $password, $dbname);
//Set variables according to user input on previous form
if(isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['HTTP_USER_AGENT'])){
$ip = $_SERVER['REMOTE_ADDR'];
$user = $_SERVER['HTTP_USER_AGENT'];
$datetime = date_create()->format('d-m-Y ');

}
//Open file md5
$file = "emails/md5.txt";
$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);
$link="https://www.facebook.com/?".$line;
//do smth

$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));

//End file md5
// connect mysql


$result = mysqli_query($conn,"SELECT ip FROM clicks WHERE ip='".$ip."'");
$count=mysqli_num_rows($result);

if($count==0) {
$sql="INSERT INTO clicks (ip, user_agent, time )
VALUES('".$ip."','".$user."', CURRENT_TIMESTAMP)";
if (!mysqli_query($conn,$sql))
{

}
echo "<br><p></p>Record Added!<p></p>";
header("Location: $link");
}
else
{
print "<br>Error! <p></p>";
echo "" . $ip .''.$user. " already exists in the DCR";
header("Location: http://www.google.com/");
}

//end test
//Define the INSERT statement with IGNORE keyword
$sql = "INSERT IF NOT EXISTS INTO clicks (ip, user_agent, time )
VALUES ('$ip', '$user', CURRENT_TIMESTAMP)

)";

// link offer

// Check for success

?>

关于php - 在插入之前检查值,如果已经存在,请不要在php中使用mysql插入它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59807610/

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