kanz Posted February 26, 2015 Share Posted February 26, 2015 hiI want to use url attachments for other post instead upload files again.Duplicate row(s) of attachments table with "attid field" posted from form and change a "postid field" in same table.I have a form with some input checkbox.The values of input are numbers which point to values of the field in database(attachments table > attid field ). <input type="checkbox" name="attid[]" value="10" /> <input type="checkbox" name="attid[]" value="250" /> This "attid" field is a Primary Key and AUTO_INCREMENT.I want when form submit, duplicate a row(s) with "attid" posted.Used this > INSERT INRO - SELECT query with loop by for eachbut not succesful $sql = ('INSERT INTO attachments (field1, field2) (SELECT * FROM attachments WHERE attid= :attid)'); thanks Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/ Share on other sites More sharing options...
Barand Posted February 26, 2015 Share Posted February 26, 2015 You cannot have two rows with the same primary key which is what you are attempting to do. Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506805 Share on other sites More sharing options...
kanz Posted February 26, 2015 Author Share Posted February 26, 2015 thanks Barand,1 field is primary keyIn "loop rows" automaticaly auto-increment the field. For example, when we are attempting attach 10 files to a post, is this have a problem? Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506830 Share on other sites More sharing options...
Barand Posted February 26, 2015 Share Posted February 26, 2015 mysql> SELECT * FROM attachments; +--------+--------+ | field1 | field2 | +--------+--------+ | 1 | abc | | 2 | def | +--------+--------+ What you are effectively attempting to do is mysql> INSERT INTO attachments (field1, field2) VALUES (1, 'xyz'); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' It will only auto_increment if you don't try to provide a value mysql> INSERT INTO attachments (field2) VALUES ('xyz'); Query OK, 1 row affected (0.00 sec) mysql> select * from attachments; +--------+--------+ | field1 | field2 | +--------+--------+ | 1 | abc | | 2 | def | | 3 | xyz | +--------+--------+ Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506837 Share on other sites More sharing options...
kanz Posted February 26, 2015 Author Share Posted February 26, 2015 I'm using field1 field2 for other field not mean the primary key.Ok, you right but i just want to duplicate a row of table according to "attid" posted not attempting to insert attid, this is primary key and auto-increment, not need to insert it by query. Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506868 Share on other sites More sharing options...
kanz Posted February 26, 2015 Author Share Posted February 26, 2015 it's my code $Post_attid = $_POST[attid]; try { $sql = ('INSERT INTO attachments (attstatus, postsID, atturl, atttype, attcaption, attordering, attwidth_incontents, itsfirst, itssecond) (SELECT * FROM attachments WHERE attid = ?)'); $stmt = $conn->prepare($sql); $stmt->bindValue(1, $Post_attid, PDO::PARAM_INT); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($results as $row) { $stmt->execute($row); } } catch(PDOException $e) { echo $e->getMessage(); } Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506880 Share on other sites More sharing options...
Barand Posted February 26, 2015 Share Posted February 26, 2015 DON'T use "SELECT * ", it will include the id field. Always specify the fields you want to select. Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506882 Share on other sites More sharing options...
kanz Posted February 26, 2015 Author Share Posted February 26, 2015 right, Where is the problem in my code, you can answer my question? Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506883 Share on other sites More sharing options...
Barand Posted February 26, 2015 Share Posted February 26, 2015 You can read? I already told you. Do not use SELECT *. The table has an id field that is not in the list of fields to insert, therefore by using * you are trying to insert at least one more field than the number of fields specified. Quote Link to comment https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/#findComment-1506885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.