Jump to content

Inserting values from a dynamic drop down list into Database


tahakirmani

Recommended Posts

Hello

 

I am trying to insert values in my database from a dynamic drop down list. I have created the following program, its not given me any error message, but also its not inserting any record in database. Kindly check it and tell me where is the problem.

I have also echo the fields which i am inserting in database, and all the fields are entering correctly in variables.

<!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>
</body>
</html>

<?php 

session_start();
if(isset($_SESSION['username']))
{
		

	include 'connect.php';
	
			$select_query= 			'Select * from category';
			$select_query_run =		mysql_query($select_query);
	
	echo "	
		<form action='insert_product.php' method='POST' ></br>
		
		Product Name: 	<input type='text' name='product_name'  /></br>
		
		Price		:	<input type= 'text' name= 'price'  /></br>
		
		Description	:	<input type='text' name='description'  />*Seperate by Comma</br>
		
						";
	
	
	
	//  Drop Down Display	
			
		


			echo "<select name='category'>";
			
			
			while	($select_query_array=	mysql_fetch_array($select_query_run) )
			{
				
					echo "<option value='".$select_query_array['category_id']."' >".
					htmlspecialchars($select_query_array["name"])."</option>";
					
				
				}
				
		 $selectTag= "<input type='submit' value='Insert'  /></select></form>";
		
		 echo $selectTag;
	//Drop Down End!
	
	
	
	
	if(isset($_POST['product_name']) && isset($_POST['price']) && isset($_POST['description']) 	)
	{
		echo $product_name	= 		$_POST['product_name'];
		echo $price			=		$_POST['price'];
		echo $description	=		$_POST['description'];
		echo $category		=		$_POST['category'];
		
			
//Problem Area 

	$query= "insert into products (name, price, description,  category_id ) 
				VALUES( '$product_name', $price, '$description', $category )";

	
	if($query_run=		mysql_query($query))
	{
				
		echo 'Data Inserted';
		
		
		
		}	
		else
		{
			'Error In SQL'.mysql_error();
			}
	}
	
	else
	{
		echo 'empty Field';
		}

}

else
{
	echo 'You Must Log in To View this Page!';
	}
?>


In your insert line, you forgot to enclose the last variable in single quotes. (not sure if that is the only issue)

No quotes if numeric value, and $category_id probably is, as is the $price

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.