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 ).
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)');
Yes, Values wasn’t right. Did you try specific fields as I suggested?
[quote=“Drummin, post:7, topic:113944”]
$sql = (‘INSERT INTO attachments (field1, field2) SELECT field1, field2 FROM attachments WHERE attid= :attid)’;
[/quote]I just tried this and it worked.
If you need to duplicate a row in same table with unique keys you can do that
Obtain the last unique key
Sum 1 to last key
execute INSERT SELECT ON DUPLICATE KEY
Example
$lastids=mysql_fetch_array(mysql_query(“SELECT Id_Inmueble FROM inmueble WHERE 1 ORDER BY Id_Inmueble DESC”));
$lastid=$lastids[“Id_Inmueble”]+1;
mysql_query(“INSERT INTO inmueble SELECT * FROM inmueble WHERE Id_Inmueble=”.$Id." ON DUPLICATE KEY UPDATE Id_Inmueble=".$ultimoid) or die(mysql_error());
But I did not receive the result
My query is probably wrong
I don’t know why you would want to make an extra query just to get the last attid, then increment it, then force an auto-increment field a value with ON DUPLICATE KEY UPDATE.
Simply specify the same fields on the value query as the insert.
Two use cases for array_walk in one evening. Odd. Checkboxes are arrays - you can’t pass arrays directly into a database. And executing an query in a loop of any kind is usually a bad idea. Try this…
<?php
if(isset($_GET['postid']) && is_numeric($_GET['postid'])) {
$postid = $_GET['postid'];
if(isset($_POST['attid']) && !empty($_POST['attid'])) {
try {
// Filter the checkbox values making sure they are integers.
array_walk($_POST['attid'], function(&$v) {$val = intval($val); });
// For further paranoia we'll use array filter to insure all values > 0
$attid = array_filter($_POST['attid'], function($val) { return $val > 0; });
// If we still have ids we can query.
if (count($attid) > 0 ) {
//INSERT
$sql = "INSERT INTO attachments (attstatus, postsID, atturl, atttype, attcaption, attordering, attwidth_incontents, itsfirst, itssecond) SELECT attstatus, ?, atturl, atttype, attcaption, attordering, attwidth_incontents, itsfirst, itssecond FROM attachments WHERE attid IN(".(implode(',',$attid)).")";
$stmt = $kanzconn->prepare($sql);
$stmt->bindValue(1, $postid, PDO::PARAM_INT);
$stmt->execute();
}
} catch(PDOException $e) {
echo $e->getMessage();
}
}
}
So we end up running only one query. One of these days PDO will be expanded to allow for arrays to be imploded int strings for use with SQL “in” clauses but until then they’ll continue to need to be written directly into the SQL statement as seen above which is inherently dangerous so proceed with caution when doing it.
Database id’s should be higher than 0, so postid > 0 is more accurate than empty. Also, the code I added needs to recheck for 0 members left in the array because the array_filter function will strip out all the rows if they are all invalid.