mjgdunne Posted April 24, 2008 Share Posted April 24, 2008 Hi, i have a script which stores images in my databse, except i cannot seem to extract them, when i do try it prints the path to the image file. Here is my script: <?php session_start(); ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="reff"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // image and titl and reg sent from form $myimage=$_POST['myimage']; $mymake=$_POST['mymake']; $mymodel=$_POST['mymodel']; $myreg=$_POST['myreg']; mysql_select_db("test"); mysql_query("INSERT INTO reff (make, model, registration, imgdata) VALUES ('$_POST[mymake]', '$_POST[mymodel]', '$_POST[myreg]', '$_POST[myimage]')"); echo "Data saved"; exit(); ?> Any help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/ Share on other sites More sharing options...
DeanWhitehouse Posted April 24, 2008 Share Posted April 24, 2008 i thought that you store the image on the server and then in the database store the url to it , then echo "<img src='$imagelink'>"; and $imagelink will be the url from the database Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526448 Share on other sites More sharing options...
leonglass Posted April 24, 2008 Share Posted April 24, 2008 I agree with the above poster that storing the url is a much better way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526450 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 Ok how would i go about saving the image to the server? Would i just save the link as text in the database? Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526454 Share on other sites More sharing options...
DeanWhitehouse Posted April 24, 2008 Share Posted April 24, 2008 no you need an uplaod form, search google for the code. Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526465 Share on other sites More sharing options...
DarkWater Posted April 24, 2008 Share Posted April 24, 2008 Were you storing the image as a BLOB? Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526469 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 I have the following upload code: <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Add new car</H1> </td></tr> <tr><td> <h2>Please upload a new picture</h2> <form action="carimage.php" method="post"> <input type=hidden name=MAX_FILE_SIZE value=150000> <input type=hidden name=completed value=1> Please choose an image to upload: <input type=file name=myimage><br> <br> Please enter the car make: <input name=mymake><br><br> Please enter the car model: <input name=mymodel><br><br> Please enter the car registration: <input name=myreg><br> <br> </TABLE> <TABLE BGCOLOR="#F0F8FF" WIDTH=100%> <TR><TD ALIGN="CENTER"> <input type="submit" value="Add Car"/><input type="reset" name="reset" value="Clear Form"/> </form><form action="login_success3.php" method="post"> <input type=submit value="Home"></form></TD></tr> <TR><TD><BR></TD></TR> </table> </td></tr> </body> </html> I stored the image as a blob in the database, i dont know if thats right. Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526473 Share on other sites More sharing options...
moselkady Posted April 24, 2008 Share Posted April 24, 2008 If you save images into the database (not their urls), you will need a separate script to read the image data from the database and this script will be the src of the <img> tag. For example, your HTML to display an image may look like: <img src="imgdisplay.php?make=mymake&model=mymodel®=myreg"> then the PHP script could be: <?php $mymake =$_GET['make']; $mymodel =$_GET['model']; $myreg =$_GET['reg']; $row = mysql_query("SELECT imgdata FROM reff WHER make='$mymake', model='$mymodel', registration='$myreg'"); $img = mysql_fetch_array($row); $img = $img[0]; echo $img; ?> Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526506 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 Hi thanks for your reply, i added the code you suggested as seen below, and as a result i am getting the image path, how do i go about using the img src to print out the image? Just a beginner with php. <?php session_start(); ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="reff"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $mymake =$_GET['make']; $mymodel =$_GET['model']; $myreg =$_GET['reg']; $row = mysql_query("SELECT imgdata FROM reff WHER make='$mymake', model='$mymodel', registration='$myreg'"); $img = mysql_fetch_array($row); $img = $img[0]; echo $img; ?> Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526519 Share on other sites More sharing options...
joinx Posted April 24, 2008 Share Posted April 24, 2008 Hi all... me too having same problem.. I stored the url of my images...like C:/Apache2.2/htdocs/Prod/Mouse/g9.jpg thn in my form i retrive it like that <?php $imageurl = $row['product_image'] ?> <tr> <td width="307"> <p><img src="<?php echo $imageurl; ?>" /> <?php echo $row['product_name']."<br><font color=red>Price:</font>Rs".$row['product_price'];?></p> <form id="form1" method="details.php" action="POST"> <input name="product_id" type="hidden" value="%s"/> <a href="details.php">More Info</a> </form> <form name="form" action="showcart.php" method="POST"> <input name="product_id" type="hidden" value="%s"/> <input type="submit" value="Add To cart"> <input type="hidden" name="MM_insert" value="form" /> </form> </form></td> </tr> <?php } ?> </table> other data is being printed except the image.. what should i correct? Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526527 Share on other sites More sharing options...
moselkady Posted April 24, 2008 Share Posted April 24, 2008 Hi thanks for your reply, i added the code you suggested as seen below, and as a result i am getting the image path, how do i go about using the img src to print out the image? Just a beginner with php. I hink I got confused . Looks like you are saving the image path not the image itself into the database. In this case, your script will simply get the path from the database and insert it in the <img> tag: $row = mysql_query("SELECT imgdata FROM reff WHER make='$mymake', model='$mymodel', registration='$myreg'"); $url = mysql_fetch_array($row); $url = $url[0]; echo "<img src='$url'>"; However, the url should be either in the form http://some_site.com/path/image.jpg or be in the form of a relative path from where your script resides. For example, as mentioned in the other post, if your script resides in C:/Apache2.2/htdocs/Prod and your images is C:/Apache2.2/htdocs/Prod/Mouse/g9.jpg, then the img tag should be <img src="Mouse/g9.jpg">. So, if the url is stored as "C:/Apache2.2/htdocs/Prod/Mouse/g9.jpg" then you need to trim "C:/Apache2.2/htdocs/Prod/" from it. Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-526599 Share on other sites More sharing options...
joinx Posted April 26, 2008 Share Posted April 26, 2008 Quote: For example, as mentioned in the other post, if your script resides in C:/Apache2.2/htdocs/Prod and your images is C:/Apache2.2/htdocs/Prod/Mouse/g9.jpg, then the img tag should be <img src="Mouse/g9.jpg">. So, if the url is stored as "C:/Apache2.2/htdocs/Prod/Mouse/g9.jpg" then you need to trim "C:/Apache2.2/htdocs/Prod/" from it. How to trim this? Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-527590 Share on other sites More sharing options...
moselkady Posted April 28, 2008 Share Posted April 28, 2008 try: $url = str_replace("C:/Apache2.2/htdocs/Prod/", "", $url); Quote Link to comment https://forums.phpfreaks.com/topic/102777-how-to-store-images-in-a-mysql-database/#findComment-528910 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.