Drumlegend Posted February 7, 2013 Share Posted February 7, 2013 I am still really new to php so I am sorry if I don't make much sense but what I want to be able to achieve is to retrieve images from a file directory that will be referenced in the database. Here is my code for submitting the images <?php $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Here is my sql for the created table CREATE TABLE `recipes` ( `recipeid` INT(11) UNSIGNED ZEROFILL PRIMARY KEY AUTO_INCREMENT, `recipename` VARCHAR(50) NOT NULL, `ingredients` VARCHAR(50) NOT NULL, `instructions` VARCHAR(50) NOT NULL, `imagename` VARCHAR(50) NOT NULL, `created` DATETIME NOT NULL ) Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/ Share on other sites More sharing options...
mindblown Posted February 7, 2013 Share Posted February 7, 2013 I would love to hear the answer to this one too. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1410861 Share on other sites More sharing options...
shlumph Posted February 7, 2013 Share Posted February 7, 2013 Are you asking for help in regards to inserting the image's reference in the database? That looks like an upload script. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1410862 Share on other sites More sharing options...
Drumlegend Posted February 7, 2013 Author Share Posted February 7, 2013 That is the upload script, so I have managed to upload the photo to the server but I want to retrieve it. This code might make more sense. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head><link rel="stylesheet" type="text/css" href="../style/user.css"></head> <title>Users</title> </head> <?php include('common.php'); $sqlget ="SELECT * FROM recipes"; $sqldata= mysqli_query($dbcon, $sqlget) or die('error getting data'); ?> <body> <section id="mainContainer"> <h1>test</h1> <section id="photo"> <h1>test</h1> <a href="#">Edit Photo</a> </section> <section id="ad1"> <h1>test</h1> </section> <section id="leftContainer"> <h1>Saved Recipe</h1> <input type="Submit" Value="View more recipes"/> </section> <section id="ad2"> <h1>test</h1> </section> <section id="middleContainer"> <?php echo "<table>"; echo "<tr><th>ID</th><th>Recipe Name</th><th>Ingredients</th><th>Instructions</th></tr>"; while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) { echo "<tr><td>"; echo $row['recipeid']; echo "</td><td>"; echo $row['recipename']; echo "</td><td>"; echo $row['ingredients']; echo "</td><td>"; echo $row['instructions']; echo "</td></tr>"; } echo "</table>"; ?> <h1>About Me</h1> </section> </section> </body> </html> I basically want each recipe to display an image Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1410864 Share on other sites More sharing options...
shlumph Posted February 7, 2013 Share Posted February 7, 2013 Your upload script does not insert the images file location into the recipes table. Unless you left that out? Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1410865 Share on other sites More sharing options...
Drumlegend Posted February 7, 2013 Author Share Posted February 7, 2013 I did leave that out as I'm not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1410866 Share on other sites More sharing options...
shlumph Posted February 8, 2013 Share Posted February 8, 2013 Oh ok. Yes, you'll need them in the database first, before you display them Depending on if the recipe is already in the database or not, you'll need to either UPDATE or INSERT. Is the recipe already in the database when submitting the image? Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1411003 Share on other sites More sharing options...
Drumlegend Posted February 11, 2013 Author Share Posted February 11, 2013 I don't want store the images in the database, I know you can do that but it is not practical for what I'm doing as I am storing multiple recipes. The recipe is already stored in the database. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1411838 Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 How do you know what recipe the image goes with? UPDATE that recipe's row. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1411865 Share on other sites More sharing options...
Drumlegend Posted February 12, 2013 Author Share Posted February 12, 2013 How do you know what recipe the image goes with? UPDATE that recipe's row. That's the question I am after, I'm not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1411921 Share on other sites More sharing options...
shlumph Posted February 12, 2013 Share Posted February 12, 2013 Hopefully this helps: $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); // Connect to database include('common.php'); // Assuming it's here? ($dbcon) $sql = "UPDATE recipes SET imagename = '{$_FILES['file']['name']}' WHERE recipeid= '{$_GET['recipeid']}'"; // Assuming $_GET is set like this? mysqli_query($dbcon, $sql) or trigger_error(mysql_error()); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1411962 Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 How do YOU the person using this system, know what image goes with what recipe? Quote Link to comment https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/#findComment-1411967 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.