0

How to insert an image as an attachment in the wordpress table "wp_posts" using sql?

I have used php and sql to try to change the already existent IDs with other IDs from the wp_posts

$update = "UPDATE wp_posts SET ID=$id_new WHERE ID=$id_old";
if (mysqli_query($conn, $update)) {
    echo "Record updated successfully"."</br>";
} else {
    echo "Error updating record: " . mysqli_error($conn)."</br>";
}

I expect id_old (which is the ID of the attachment) to be changed with id_new, but when i access the attachment link, instead of showing the image, it displays the title of the image.

Eddy
  • 23
  • 5
  • Don't use string concatenation to build SQL queries, you get [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) vulnerabilities. You should use prepared statements with bound parameters, via either [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php). [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky May 09 '19 at 16:29
  • Attachments store information in `wp_postmeta` table as well, so you might need to update it too. I'd use the [API](https://codex.wordpress.org/Function_Reference/wp_insert_attachment) if possible. – msg May 09 '19 at 19:34

0 Answers0