1

您好我正在尝试通过选择复选框将信息数组插入数据库中的字段中

    $list = $_POST['sub'];

    // for each loop to insert each value into the database with the selected users informtion
    foreach ($list as  $value)  {

        // The query to run
        $listQuery='INSERT INTO tbl_list (`userId`, `subId`) VALUES (\'' . $id . '\', \'' . $value . '\')';


        // Run the query
        $objects->query($listQuery);


    }
4

4 回答 4

2

userId您应该为 ( , )添加一个唯一键subId

ALTER TABLE tbl_list ADD UNIQUE(`userId`, `subId`)

然后,您应该使用INSERT IGNOREorREPLACE INTO来避免插入期间的错误。

于 2013-03-21T07:07:24.703 回答
0

您可以使用 Insert Ignore而不是Insert在 mysql 查询中

于 2013-03-21T07:12:35.523 回答
0

为了阻止重复的条目进入数据库,你必须做这件事。一步一步来

> 1.set a unique key on the table

after Complete create unique key you have to decide what you want to do when there's a duplicate

> 2. ignore it

> 3.Overwrite the previously entered record

> Update some counter
于 2014-01-28T18:45:07.207 回答
-1

您需要做两件事,首先使您的 userId 成为主键,然后尝试此查询
$listQuery='INSERT INTO tbl_list ( userId, subId) VALUES (\'' . $id . '\', \'' . $value . '\' ) 重复键更新 userId = LAST_INSERT_ID()';

于 2013-03-21T07:04:59.450 回答