Jump to content

Duplicate one or some rows in PDO


kanz

Recommended Posts

hi
I 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 each
but not succesful

 

$sql = ('INSERT INTO attachments (field1, field2)  (SELECT * FROM attachments WHERE attid= :attid)');

thanks

Link to comment
Share on other sites

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    |
+--------+--------+
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.