I need to grab images from an URL then change the URL into another. Here is my code for that. But it is not working as I expected. It is do change only one image. Others won't.
Here is my code,
<?php
include_once('db-conn.php');
$query = "SELECT my_image_url AS image FROM my_image";
$result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$img_url = $row['image'];
$content = file_get_contents($img_url);
$img_name = basename($img_url);
file_put_contents($img_name, $content);
$query = "UPDATE `my_image` SET `my_image_url` = 'http://localhost/img/".$img_name."'";
$result2 = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);
}
}
?>
whereclause.wherecondition in your update query to update particulate rowSELECT my_image_url AS image, id FROM my_imagethen use theidto update the particular row. You also should use prepared statements, this could potentially get you injected.