I want to upload multiple image name in a same row using PHP MYSQL -
here code
for ($i = 0; $i < count($_files["user_files"]["name"]); $i++) { // image mime type $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_files["user_files"]["tmp_name"][$i]))); if (in_array($image_mime, $valid_image_check)) { $foldername = "uploads/"; $ext = explode("/", strtolower($image_mime)); $ext = strtolower(end($ext)); $filename = rand(10000, 990000) . '_' . time() . '.' . $ext; // if user upload file abc,jpg, convert 291905_1399918178.jpg based on random number , time. $filepath = $foldername . $filename; if (!move_uploaded_file($_files["user_files"]["tmp_name"][$i], $filepath)) { echo "fail uplaod"; } else { $smsg .= "<strong>" . $_files["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>"; $magicianobj = new imagelib($filepath); $magicianobj->resizeimage(100, 100); $magicianobj->saveimage($foldername . 'thumb/' . $filename, 100); } } else { echo "not image"; } }
and mysql query
$sql = "insert properties (agent_id, property_name, category, location, property_type, search_radius, price, bed_rooms, bath_rooms, commercial_type, area, address, description, image_name, date_added) values ('$agent_id', '$property_name', '$listing_for', '$city', '$property_type', '$area', '$price', '$beds', '$baths', '$commercial_type', '$area_sf', '$address', '$description', '".$filename."', now() )" ;
so here need insert $filename
values in single row.
but when run script last $filename
inserted db.
how can insert values of $filename
loop in db in single row.
you'll need use persistent array this.
$filenames = array(); ($i = 0; $i < count($_files["user_files"]["name"]); $i++) { // image mime type $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_files["user_files"]["tmp_name"][$i]))); if (in_array($image_mime, $valid_image_check)) { $foldername = "uploads/"; $ext = explode("/", strtolower($image_mime)); $ext = strtolower(end($ext)); $filename = rand(10000, 990000) . '_' . time() . '.' . $ext; // if user upload file abc,jpg, convert 291905_1399918178.jpg based on random number , time. $filepath = $foldername . $filename; if (!move_uploaded_file($_files["user_files"]["tmp_name"][$i], $filepath)) { echo "fail uplaod"; } else { $smsg .= "<strong>" . $_files["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>"; $magicianobj = new imagelib($filepath); $magicianobj->resizeimage(100, 100); $magicianobj->saveimage($foldername . 'thumb/' . $filename, 100); $filenames[] = $filepath; } } else { echo "not image"; } }
and in sql:
$images = rtrim(implode(',', $filenames), ','); $sql = "insert properties (agent_id, property_name, category, location, property_type, search_radius, price, bed_rooms, bath_rooms, commercial_type, area, address, description, image_name, date_added) values ('$agent_id', '$property_name', '$listing_for', '$city', '$property_type', '$area', '$price', '$beds', '$baths', '$commercial_type', '$area_sf', '$address', '$description', '$images', now() )" ;
this insert comma separated list of filenames db.
you may want have @ prepared statements looks of query.
you should use table this, attaches property id.
image_id | image_url | property_id select `image_url` `property_images` `property_id` = :property_id
something along lines, has been commented bad way of doing things.
Comments
Post a Comment