简体   繁体   English

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

[英]Check value before insert and if already exists don't insert it on mysql using php

I have code check ips addresses if duplicated will redirected to another link but the code is running good when insert value in ( ip,user_agent,time ) in table ( click ) first time but when i try to check if running with some value it written again on database I need any value ( ip,user_agent,time ) write just one time after written to database not written the some value ( ip,user_agent,time )我有代码检查 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");
        }
    ?> 

I have code check ips addresses if duplicated will redirected to another link but the code is running good when insert value in ( ip,user_agent,time ) in table ( click ) first time but when i try to check if running with some value it written again on database I need any value ( ip,user_agent,time ) write just one time after written to database not written the some value ( ip,user_agent,time )我有代码检查 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.

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