[英]Check value before insert and if already exists don't insert it on mysql using php
我有代碼檢查 ips 地址,如果重復將重定向到另一個鏈接,但是當第一次在表( click
)中插入值( ip,user_agent,time
)時代碼運行良好,但是當我嘗試檢查是否使用某些寫入的值運行時再次在數據庫上我需要任何值( 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");
}
?>
我有代碼檢查 ips 地址,如果重復將重定向到另一個鏈接,但是當第一次在表( click
)中插入值( ip,user_agent,time
)時代碼運行良好,但是當我嘗試檢查是否使用某些寫入的值運行時再次在數據庫上我需要任何值( 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");
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.