Adding an image into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Adding an image into database

Post by beginner123 »

I am making a webiste selling musical instruments, and I have a form I made to add the products into the database.
My problem is when I add an image I have to use code eg (<img src="images/drums.gif">) into the form text box but I want to just be able to enter the name of the image instead of the code.

Code for form:

Code: Select all

 <?php 

				@$db = new mysqli( 'localhost', 'root', "", 'k00127082'); 
					
				if (mysqli_connect_errno()) { 
				echo 'error connecting to db'; 
				exit; 
				} 
					
				$product_name=$_POST["product_name"];
				$id=$_POST["id"];
				$product_description=$_POST["product_description"];
				$quantity_on_hand=$_POST["quantity_on_hand"];
				$price=$_POST["price"];
				$image=$_POST["image"];
				$image_big=$_POST["image_big"];
					
				$query = "INSERT INTO products ( product_name, id, product_description, quantity_on_hand, price, image, image_big ) 
					VALUES('$product_name', '$id', '$product_description', '$quantity_on_hand', '$price', '$image', '$image_big' )"; 
					
					$result = $db->query($query); 
					
					if($result) 
					echo $db->affected_rows . ' products inserted into database'; 
					else 
					echo 'There was a problem inserting the information into the database'; 
					
					$db->close(); 
					
				?>
The code for the form is made in html and is linked to this page. Everything works fine I just want to fix the problem with the adding the image
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Adding an image into database

Post by Christopher »

It sounds like you need to change the code that displays the the image on the product page, from this:

<?php echo $image; ?>

to this:

<img src="<?php echo "$image; ?>">
(#10850)
Post Reply