Jump to content

How to store images in a mysql database


mjgdunne

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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&reg=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

 

Link to comment
Share on other sites

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;
?> 

:)

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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  :D. 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.

 

 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.