Jump to content

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
https://forums.phpfreaks.com/topic/294913-duplicate-one-or-some-rows-in-pdo/
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    |
+--------+--------+

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.

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();
}

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.

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.