Hello, Would someone please help. I am unsure how to upload form which has several text fields and an image attachment in to the database. Image is stored in the database - blob. I would also like to add details of the user after login. UserID (thorugh the session) Here is my current code: Only uploads image at the moment. DATABASE TABLE CREATE TABLE Items_wanted ( RefID int not null auto_increment primary key, Sub_ID int references Subscriber (SubID), Cat_ID int references Categories (CatID), Title varchar(20) NOT NULL, Category varchar(40) NOT NULL, Description text NOT NULL, timeStamp timestamp NOT NULL, Location varchar (20)) type = MyISAM AUTO_INCREMENT=1; FORM: <form enctype="multipart/form-data" action="insert.php" method="post" name="changer"> <table> <tr> <td> Title of item* </td> <td> <input type = "text" name= "title" value ="" size="25" maxlength="100" /> </td> </tr> <tr> <td> Category* </td> <td> <select name="category" value ="" id="category"> <option value="">Select a category</option> <option value="1">Accessories</option> <option value="2">Antiques</option> <option value="3">Bikes</option> <option value="4">Books</option> <option value="5">Cars</option> <option value="6">Children stuff</option> <option value="7">Computing</option> <option value="8">Clothes & Shoes</option> <option value="9">DIY</option> <option value="10">Electronics</option> <option value="11">Furniture</option> <option value="12">Gardening</option> <option value="13">Hobbies</option> <option value="14">Music & Films</option> <option value="15">Phones</option> <option value="15">Miscellenious</option> </select> </td> </tr> <tr> <td> Location* </td> <td> <input type = "text" name= "title" value ="" size="25" maxlength="100" /> </td> </tr> <tr> <td> Description* <p>Your description<br /> should include <br /> relevant details <br /> such as make,<br /> condition etc.</p> </td> <td> <textarea id="description" name="description" value ="" cols="30" rows="10"></textarea> </td> </tr> <tr> <td> <input name="MAX_FILE_SIZE" value="102400" type="hidden"> <input name="image" accept="image/jpeg" type="file"> </td> <td colspan="6"> <input value="Upload" type="submit"> </td> </tr> <tr> <td colspan="6"> <input value="Submit" type="submit"> </td> </tr> </table> </form> insert.php <?php /* database connection*/ $username = "root"; $password = ""; $host = "localhost"; $database = "mydatabase"; $link = mysql_connect($host, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db ($database); /* error checking*/ $title =$_POST['title']; $category =$_POST['category']; $location =$_POST['location']; $description =$_POST['description']; /* Check all the details have been inputed */ $curnum =0; if(!$title) { $curnum ++; echo $curnum . ". You didn't enter title!<Br>\n"; } if(!$category) { $curnum ++; echo $curnum . ". You didn't choose any category!<Br>\n"; } if(!$location) { $curnum ++; echo $curnum . ". You didn't enter any location!<Br>\n"; } if(!$description) { $curnum ++; echo $curnum . ". You didn't enter any description of the item!<Br>\n"; } /* temporary file stored on the server*/ $tmpName = $_FILES['image']['tmp_name']; /* read the file*/ $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); /* create the query and insert image in to database*/ $query = "INSERT INTO items_to_give "; $query .= "(image) VALUES ('$data')"; $results = mysql_query($query, $link); /* print thank you sentence*/ print "The details have been uploaded and will be displayed shortly."; /* close database connection*/ mysql_close($link); ?> I just do not know where to include the query to insert the text fields in to the database together with the image. Would someone please help. Many Thanks PinkyM
One tip, please donot store the image into the database. Database is for indexing certain stuff HDD is for putting big amounts of data for one file (for example images).
I noticed that you have two tables namely *items_wanted - where the text is *items_to_give - where you upload the image I also noticed that you are missing some of the variables that should hold a value for the following *Sub_ID *Cat_ID *timestamp Solution: 1. Declare the variables and assign their values - I don't have any idea where will you get the values for Sub_ID and Cat_ID - for timestamp you can use this $time = time(); 2. Insert this codes $query2 = "INSERT INTO items_wanted VALUES('','$your variable for Sub_ID','$your variable for Cat_ID','$title','$category','$description','$time','$location')"; $results2 = mysql_query($query2, $link); I hope it works..
Hello Renzmar, many thanks, but I have confused you by pasting incorrect table in. Here is my whole database structure: DATABASE TABLES: CREATE TABLE Subscriber ( SubID int not null auto_increment primary key, Name varchar(40) not null, Surname varchar(40) not null, Email varchar(50) not null, UserID varchar(20) not null, Password varchar(20) not null) type = MyISAM AUTO_INCREMENT=1; CREATE TABLE Items_to_give ( RefID int not null auto_increment primary key, Sub_ID int references Subscriber (SubID), Cat_ID int references Categories (CatID), Title varchar(20) NOT NULL, Description text NOT NULL, Location varchar (20), timeStamp timestamp NOT NULL, image blob) type = MyISAM AUTO_INCREMENT=1; CREATE TABLE Items_wanted ( RefID int not null auto_increment primary key, Sub_ID int references Subscriber (SubID), Cat_ID int references Categories (CatID), Title varchar(20) NOT NULL, Category varchar(40) NOT NULL, Description text NOT NULL, timeStamp timestamp NOT NULL, Location varchar (20)) type = MyISAM AUTO_INCREMENT=1; CREATE TABLE md_categories ( cat_id int(10) unsigned NOT NULL auto_increment, cat_name varchar(64) NOT NULL default '', cat_order int(2) unsigned NOT NULL default '0', created timestamp NOT NULL, PRIMARY KEY (cat_id) ) TYPE=MyISAM AUTO_INCREMENT=1 ; Subsriber table- allows users to register. I also have a login page before displaying a page where the user can add an item. Tables Items_to_give and Items_wanted are literally the same, with the difference that items_wanted does not include an image. ( I guess all this could be in one table, but I wouldn't know how to get it out properly). It is meant to separate te posts according to whether the people ar posting advert to get rid of an item or or they ar looking for an item. The sub_ID will need to be brought accross from the session- which will be is created after user logs in. The cat_ID is meant to be brought from md_categories table- each category has a number. The rest is same the form and insert.php. Hope this makes a better sense. I am a complete novice to php an really approciate your help! Thank you again. Pinky M