eZe616 Posted May 21, 2007 Share Posted May 21, 2007 I have a multiple image up loader to upload images to a server. Now while it uploads the file I want it the insert the link to the image in the database. What I have works, but not exactly how I want. I have a table called hpics with 10 fields, 1 of them is called id, which in turn is not auto_inceremented, because I want to insert the id. Now, I'm trying the insert all 9 images, when uploaded, with 1 row with an id. Instead it uploads the images as seperate rows, instead of the fields This is my code: while(list($key,$value) = each($_FILES['pictures']['name'])) { if(!empty($value)) { $filename = $value; $add = "upload/$filename"; if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key], $add)) { $i = 1; $sql = "INSERT INTO hpics SET p".$i." = '".$add."'"; $insert = mysql_query($sql) or die("<b>Error</b>: " . mysql_error()); ++$i; } } } Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/ Share on other sites More sharing options...
colombian Posted May 21, 2007 Share Posted May 21, 2007 You may want to clarify exactly what you need. Becuase it sounds like you just need to create 2 tables. One for images and URL, and one for the rest of the data. You could get fancy and use InnoDB tables and enforce a foreign key and all that stuff. That is, if you want a seperate URL per image. You want all the images in the same row, per submission, and the URL of a page that displays the multiple images? (seemed like this was your goal) If you are uploading the whole data (not just name, but every bit of information, you'll have to have multiple fields - as many as you allow images to be uploaded - but be warned that the database could get bogged down and huge in a short time. It'll just means a lot of fields have no data if they just upload one pictures. If you want to keep it all in a single field, then you could just strip that array as comma separated or space separated, and call them all (granted that you upload them to your server and NOT the SQL database) Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/#findComment-258632 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 Use update!!.. i don't understand when uploaded, with 1 row with an id. Instead it uploads the images as seperate rows, instead of the fields Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/#findComment-258635 Share on other sites More sharing options...
eZe616 Posted May 21, 2007 Author Share Posted May 21, 2007 Well. I'm building a CMS system for a realestate site, and want the admin, to upload the data, and the images in one try. I do have 2 table, one for data and one for pictures. What i'm trying to get is that, when the data gets inserted into the data table, it's ID is auto_incremented. I'll get that ID #, just inserted, with LAST_INSERT_ID() and then insert all the images url, into the row asigned the ID i just got.with the technique described here http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html INSERT INTO foo (auto,text) VALUES(NULL,'text'); # generate ID by inserting NULL INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text'); # use ID in second table just, with the script I have, it inserts every image in it's own row, instead of 1 row with all fields p1-p9 . Or is it just easier to assign each their own row, with the ID?? Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/#findComment-258646 Share on other sites More sharing options...
colombian Posted May 21, 2007 Share Posted May 21, 2007 The script you have above only talks about 1 table, the image one. And it is creating a row per image, so it's probably easier to leave it as is, 1 row per image. Obviously not the most efficient method, but isn't it already doing what you need it to do then? You could change your code by creating a string/array variable that contains all the uploaded images, then pass that into the query. So before your SQL query, have a variable that is storing every image, then run the query. Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/#findComment-258655 Share on other sites More sharing options...
eZe616 Posted May 22, 2007 Author Share Posted May 22, 2007 The script you have above only talks about 1 table, the image one. And it is creating a row per image, so it's probably easier to leave it as is, 1 row per image. Obviously not the most efficient method, but isn't it already doing what you need it to do then? You could change your code by creating a string/array variable that contains all the uploaded images, then pass that into the query. So before your SQL query, have a variable that is storing every image, then run the query. Yeah it tallks about only one, since i'll be running this query after the 1st data one has finished, but i'm only using this one now, since I want to make sure I got this part, before I move on. I have no Idea how I would creat an array from a multiupload file Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/#findComment-258699 Share on other sites More sharing options...
eZe616 Posted May 22, 2007 Author Share Posted May 22, 2007 This is the code I have so far for uploading the data AND the pictures. <?php include 'dbcon.php'; $name = $_POST['name']; $lname = $_POST['lname']; $type = $_POST['type']; $address= $_POST['address']; $city = $_POST['city']; $area = $_POST['district']; $dscpt = $_POST['dscpt']; $bedr = $_POST['bedr']; $bathr = $_POST['bathr']; $story = $_POST['story']; $sqft = $_POST['sqft']; $yearb = $_POST['yearb']; $status = $_POST['status']; $awg = $_POST['awg']; $usd = $_POST['usd']; $floor = $_POST['flooring']; $cooling= $_POST['cooling']; $garage = $_POST['garage']; $swpool = $_POST['swpool']; if( empty($type) && empty($status) ) { echo "Please Fill in the type/status fields"; die(); } $sql = "INSERT INTO house SET name='".$name."', lastn='".$lname."', usd='".$usd ."', price_AWG='".$awg."', address='".$address."', area='".$area."', sqft='".$sqft."', yearb='".$yearb."', status='".$status."', type='".$type."', story='".$story."', bedr='".$bedr."', bathr='".$bathr."', flooring='".$floor."', cooling='".$cooling."', garage='".$garage."', swpool='".$swpool."', dispr='".$dscpt."'"; $insert = mysql_query($sql) or die("<b>Error</b>: " . mysql_error()); $id = mysql_insert_id(); foreach ( $_FILES["pictures"]["error"] as $key => $error ) { if ( $error == UPLOAD_ERR_OK ) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; $path = "upload/$name"; if(move_uploaded_file($tmp_name, $path)) { $sql = "INSERT INTO hpics SET id='".$id."', p1='".$path."'"; $result = mysql_query($sql) or die("Error"); echo "<img src=\"".$path."\" alt=\"\" /><br />"; } } } ?> For some reason the picture uploader works fine when it's on its own, but when I use the script above, it inserts the data, but does nothing with the pictures. I don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/52405-inserting-image-url-into-mysql-in-a-while-loop/#findComment-258790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.