drc4925 2011-10-29 01:22
浏览 251
已采纳

PHP / MYSQL - 在插入新记录之前检查它是否有重复记录

Suppose I have a table called "device" as below:

device_id(field)
123asf15fas
456g4fd45ww
7861fassd45

I would like to use the code below to insert new record:

...
$q = "INSERT INTO $database.$table `device_id` VALUES $device_id";
$result = mysql_query($q);
...

I don't want to insert a record that is already exist in the DB table, so how can I check whether it have duplicated record before inserting new record?

Should I revise the MYSQL statement or PHP code?

Thanks

UPDATE

<?php
    // YOUR MYSQL DATABASE CONNECTION
    $hostname = 'localhost';
    $username = 'root';
    $password = '';

    $database = 'device';
    $table = 'device_id';
    $db_link = mysql_connect($hostname, $username, $password);
    mysql_select_db( $database ) or die('ConnectToMySQL: Could not select database: ' . $database );
    //$result = ini_set ( 'mysql.connect_timeout' , '60' );

    $device_id = $_GET["device_id"];
    $q = "REPLACE INTO $database.$table (`device_id`) VALUES ($device_id)";
    $result = mysql_query($q);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
?>
  • 写回答

2条回答 默认 最新

  • doudun8705 2011-10-29 01:30
    关注

    Since I understood well your question you have two ways to go, it depends how you would like to do the task.

    First way -> A simple query can returns a boolean result in the device_id (Exists or not) from your database table. If yes then do not INSERT or REPLACE (if you wish).

    Second Way -> You can edit the structure of your table and certify that the field device_id is a UNIQUE field.

    [EDITED]

    Explaining the First Way

    Query your table as follow:

    SELECT * FROM `your_table` WHERE `device_id`='123asf15fas'
    

    then if you got results, then you have already that data stored in your table, then the results is 1 otherwise it is 0

    In raw php it looks like:

    $result = mysql_query("SELECT * FROM `your_table` WHERE `device_id`='123asf15fas'");
    if (!$result)
    {
       // your code INSERT
       $result = mysql_query("INSERT INTO $database.$table `device_id` VALUES $device_id");
    }
    

    Explaining the Second Way

    If your table is not yet populated you can create an index for your table, for example go to your SQL command line or DBMS and do the follow command to your table:

    ALTER TABLE `your_table` ADD UNIQUE (`device_id`)
    

    Warning: If it is already populated and there are some equal data on that field, then the index will not be created.

    With the index, when someone try to insert the same ID, will get with an error message, something like this:

    #1062 - Duplicate entry '1' for key 'PRIMARY' 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败