Edit 2
I removed the previous scripts, here's a new one:
basically what i want to achieve is when inserting into the database i would like to store the image path with names as arrays. so that i could store as much images i want into a particular id, for example an id 1 will have 3 images, id 2 will have 5 images , id 3 will have 4 images and so on
Best way to do this is to add a separate entry for each image url. You can also use a single entry and store all the urls as an array, but since you're storing this in a database that isn't very useful. Storing each entry separately makes it a lot easier to access and update the data.
Also, make sure the "id" field in your table isn't a primary key / auto incrementing. Otherwise you won't be able to use the same id more than once. I fixed that here:
// array with your images
$images = array('url1','url2','url3');
// determine a new id
$sql = "SELECT MAX(id) FROM images";
$res = mysql_query($sql);
list($id) = mysql_fetch_row($res);
// highest current id, plus one
$id++;
// now create a values array to insert in the database
$values = array();
foreach ($images as $image) {
$values[] = "($id, '".mysql_real_escape_string($image)."')";
}
// comma-separate the values
$values = implode(',',$values);
// run the query
$sql = "INSERT INTO images (id, image_url) VALUES $values";
mysql_query($sql);
If you do want to store arrays, it's best to serialize
the data and use unserialize
when you retrieve it. Example:
$images = array(...);
$images = serialize($images);
$sql = "INSERT INTO images (image_url) VALUES ($images)";
To add images, you should then first retrieve the data, unserialize the array, update the array with the new images, serialize it again, update the database entry.