doubo1883 2016-04-09 14:32
浏览 25

避免PHP中的重复ID

EDIT: Question edited to be exact after Marcus reply

I am trying to practice library management and my page is for order where a book can be ordered as many time but same book should not be selected in two different selects on one page. On order page, I have multiple select dropdowns for book values coming from same database:

<select name="user">
<option value="1">Tony</option>
<option value="2">Gary</option>
<option value="3">Martin</option>
<option value="4">Austin</option>
<option value="5">Mark</option>
</select>

<select name="book[]">
<option value="1">Math</option>
<option value="2">Geography</option>
<option value="3">Science</option>
<option value="4">Spanish</option>
<option value="5">English</option>
</select>

<select name="book[]">
<option value="1">Math</option>
<option value="2">Geography</option>
<option value="3">Science</option>
<option value="4">Spanish</option>
<option value="5">English</option>
</select>

<select name="book[]">
<option value="1">Math</option>
<option value="2">Geography</option>
<option value="3">Science</option>
<option value="4">Spanish</option>
<option value="5">English</option>
</select>

And adding them in database table which has ID as auto increment for record and bookID to store book id. I am doing it with:

$userID = $_POST['user'];
    foreach($_POST['book'] as $key=>$item_eid){
    $bookID = intval($_POST['book'][$key]);
    mysql_query ("Insert INTO user_books (bookID, userID) values ('$bookID', '$userID' )") or die(mysql_error());
    }

I dont want to have two record of same book ID. In short, I want to avoid duplicate entries on bookID. I have tried "INSERT IGNORE INTO" but it did not work. Also, I want to show an error if duplicate entries are selected instead of just ignoring. Any thoughts on it?

Edit: Table structure:

CREATE TABLE IF NOT EXISTS user_books
(
    ID int(11) NOT NULL AUTO_INCREMENT, 
    bookID int(11) NOT NULL, 
    userID int(11) NOT NULL, 
    added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 
) 
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ; 
  • 写回答

1条回答 默认 最新

  • dtrn74832 2016-04-09 14:49
    关注

    The column needs to be UNIQUE. Right now there is no constraint on the column which is allowing it to have duplicate values. Change the column to UNIQUE:

    ALTER TABLE user_books ADD UNIQUE (bookID);
    

    This query will fail, however, if you already have duplicate values in the bookID column. If you don't care about the (automatic) removal of duplicate entries, use the following query instead:

    ALTER IGNORE TABLE user_books ADD UNIQUE (bookID);
    

    This will keep the first found entry of any duplicates, and delete the rest, ie. Imagine having 5 entries with bookID 100. The aforementioned query will keep the first entry with bookID 100, and delete the remaining 4 automatically.

    Then you can use INSERT IGNORE INTO thereafter.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图