stublackett Posted February 19, 2008 Share Posted February 19, 2008 Hi, I have a script that uploads an image to a remote directory, But I need to know how you get the Image string added to the Database. The table is called "Ideas" and it will also insert data such as Title and Description, But thats not the issue, Its getting the image into the DB and calling it back out Any help appreciated, Cheers Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/ Share on other sites More sharing options...
dbo Posted February 19, 2008 Share Posted February 19, 2008 Are you physically storing the file in the database, aka a BLOB field? Or are you just storing the filename? Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-470479 Share on other sites More sharing options...
stublackett Posted February 19, 2008 Author Share Posted February 19, 2008 Hoping to just store the filename, Althought a BLOB is a good idea I'd just rather link to the URL String of an Uploaded file Cheers Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-470503 Share on other sites More sharing options...
dbo Posted February 19, 2008 Share Posted February 19, 2008 So pull out the filename and use it as the location of an image. ie <img src="filename/from/db.jpg" /> Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-470532 Share on other sites More sharing options...
aschk Posted February 19, 2008 Share Posted February 19, 2008 When you grab information from PHP after an uploaded you'll have available the $_FILES[] superglobal. I believe what you're after is $_FILES['PUT YOUR FIELD NAME HERE']['name']. Put that into a variable and stick it in your database, simple! See http://uk3.php.net/features.file-upload for further information. Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-470595 Share on other sites More sharing options...
stublackett Posted February 19, 2008 Author Share Posted February 19, 2008 Simple ??? ? I'm struggling with the concept of that and where to put it I've got my script setup, Its inserting all the Variables into the DB Table other than that of the image string or anything, I've tried a few chops and changes as shown on that UK2.php.net/ site <?php include ("dbconnect.php"); # Connect to PHPMyAdmin DB $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($dbname,$db); ?> <?php #Script idea-submit.php //Collect Forms' Post Values $title = $_POST['title']; $description = $_POST['description']; $category = $_POST['category']; $img = $_FILES['image']['name']; //Validation Check //Check Ideas' Title if (!empty($_POST['title'])) { $title = ($_POST['title']); }else{ $title = NULL; echo "<p>You need to enter your ideas' title</p>"; } //Check Description if (!empty($_POST['description'])) { $description = ($_POST['description']); }else{ $description = NULL; echo "<p>You need to enter your ideas' description </p>"; } // If everything is filled out print the message. if($title && $description) { { // If all is ok, Insert into DB $sql = "INSERT INTO $db_table2(title,description,category,image) values ('$title','$description','$category','$img')"; // Incase needed($result = mysql_query($sql ,$db)); ($result = mysql_query($sql ,$db) or die(mysql_error())); echo "Thank you, For your ideas<br /> <br /> You entered:</b> <b>Title</b> : $title<br /><br /> <b>Description</b>: $description"; } ?> <?php ?> <?php } ?> Is my code Its that $img that I need to output a string to the Database! But thinking about it I'm also not uploading that image anywhere either, Whats the best way to include an Upload for the image aswell so that the database then links to the URL String of the Upload? Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-470742 Share on other sites More sharing options...
dbo Posted February 19, 2008 Share Posted February 19, 2008 echo out your $img variable after you assign a value from the $_FILES array. It looks as though its not getting assigned a value, which probably means the name of your file form field isn't image. Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-471201 Share on other sites More sharing options...
stublackett Posted February 20, 2008 Author Share Posted February 20, 2008 the echo of $img didnt output anything at all out to the screen So if thats the case I'll put the code for the ideas.php page on here, Which is the form that this collects the data from It is as follows (I'm not even 100% sure that submitting this form as it is, Is uploading the file, I need it to go to an folder titled 'ideas' and the URL String to be output into the Database <?php session_start(); include ("dbconnect.php"); if(!isset($_SESSION[username])){ $message = <<<HTML This page is for Registered users only <br /> <br /> Register to the site, <br /> <br /> By following the link in the menu on the left HTML; }else{ //Protected Area sits below $ideas_form = <<<FORM <form action="idea-submit.php" method="post" enctype="multipart/form-data" name="idea" id="idea"> <fieldset> <div> <label for ="ideatitle=">Idea Title : <input type="text" name="title" id="title" class="txt" /> </label> </div> <div> <label>Idea Description : <textarea name="description" id="description" class="txt" /></textarea> </label> </div> <div> <label>Idea category : <select name="category" id="category"> <option>Sports</option> <option>Retail</option> <option>Clothing</option> </select> </div> <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="__URL__" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="file" type="file" id="image" /><br><br> <input type="submit" value="Submit Your Idea" class="btn"/> </form> FORM; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-471499 Share on other sites More sharing options...
aschk Posted February 20, 2008 Share Posted February 20, 2008 As dbo said, it appears you've named your field wrongly. You field NAME (NOT id) is "file", thus you should be using $_FILES['file']['name']; Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-471823 Share on other sites More sharing options...
stublackett Posted February 20, 2008 Author Share Posted February 20, 2008 Right, Here goes I've managed to look at that and pair up the "name" from the html page, Paired that up with that in the PHP Page and the echo is "ideas/" The code is as follows : The Database is now picking up the string of "filename.jpg" in the Database, But I want it to reference to "/ideas/filename.jpg" So when I pull it in from the Database it is shown Dynamically <?php include ("dbconnect.php"); # Connect to PHPMyAdmin DB $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($dbname,$db); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php #Script idea-submit.php By Stuart Blackett - d4066435@tees.ac.uk //Collect Forms' Post Values $title = $_POST['title']; $description = $_POST['description']; $category = $_POST['category']; $img = $_FILES['image']['name']; //Validation Check //Check Ideas' Title if (!empty($_POST['title'])) { $title = ($_POST['title']); }else{ $title = NULL; echo "<p>You need to enter your ideas' title</p>"; } //Check Description if (!empty($_POST['description'])) { $description = ($_POST['description']); }else{ $description = NULL; echo "<p>You need to enter your ideas' description </p>"; } //Handle the Image //Set Images Upload Directory $uploaddir = "/ideas"; // Upload Image if(is_uploaded_file($_FILES['image']['tmp_name'])) { move_uploaded_file($_FILES['image']['tmp_name'],$uploaddir.''.$_FILES['file']['name']); } if($title && $description) { { // If all is ok, Insert into DB $sql = "INSERT INTO $db_table2(title,description,category,image) values ('$title','$description','$category','$img')"; // Incase needed($result = mysql_query($sql ,$db)); ($result = mysql_query($sql ,$db) or die(mysql_error())); echo "Thank you, For your ideas<br /> <br /> You entered:</b> <b>Title</b> : $title<br /><br /> <b>Description</b>: $description"; echo $uploaddir; } } ?> </body> </html> I tried editing this post this morning to update you on how I was getting on, But it wouldnt let me :'( :'( Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-471840 Share on other sites More sharing options...
aschk Posted February 20, 2008 Share Posted February 20, 2008 Going by your use of $_FILES['image']['tmp_name'] (note image NOT file as it should be) your file is not being properly moved from it's temporary hold? Also, surely to have the full path to the image, all you need is to change your $img to include the directory before you insert it into your SQL. e.g. $img = $uploaddir."/".$img; Quote Link to comment https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/#findComment-471867 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.