mysql - How do i store array of image names dynamically into the database using PHP -
i using jquery plugin uploadify upload files, , store image name path database simple mysql query , retrieve it.
my mysql database table using following entities,
id int(20) image_url varchar(255)
my image store procedure such as, first rename file, give unique name , move desired directory. , store image name path database.
currently using simple mysql insert statement storing single file name.
now want upload array of image names path. or want store image want dynamically single table col image_url, reason plan store 4 or more images, want 4 image have 1 particular id number.
how do it?
thank you
edit 2
removed previous scripts, here's new one:
basically want achieve when inserting database store image path names arrays. store images want particular id, example id 1 have 3 images, id 2 have 5 images , id 3 have 4 images , on
best way add separate entry each image url. can use single entry , store urls array, since you're storing in database isn't useful. storing each entry separately makes lot easier access , update data.
also, make sure "id" field in table isn't primary key / auto incrementing. otherwise won't able use same id more once. fixed here:
// array images $images = array('url1','url2','url3'); // determine new id $sql = "select max(id) images"; $res = mysql_query($sql); list($id) = mysql_fetch_row($res); // highest current id, plus 1 $id++; // create values array insert in database $values = array(); foreach ($images $image) { $values[] = "($id, '".mysql_real_escape_string($image)."')"; } // comma-separate values $values = implode(',',$values); // run query $sql = "insert images (id, image_url) values $values"; mysql_query($sql);
if do want store arrays, it's best serialize
data , use unserialize
when retrieve it. example:
$images = array(...); $images = serialize($images); $sql = "insert images (image_url) values ($images)";
to add images, should first retrieve data, unserialize array, update array new images, serialize again, update database entry.
Comments
Post a Comment