hi i am writing a program which take the value from form and then execute a error "failed" each time. which means there is some error in my query but i cant find the problem. here is the code

<?php 
session_start();
//print_r($_POST);
$name=$_SESSION['SESS_CUSTOMER_ID'];

$con=mysql_connect("localhost","root","");
 
	mysql_select_db("pras2");
	
	$product_id=$_POST['P_list'];
	echo "$product_id";
	$quantity=$_POST['quantity'];
	
	$query= "INSERT INTO order (customer_ID, product_id, quantity) VALUES ('$name', '$product_id', '$quantity')";
//die($query);
	$result=mysql_query($query);
	
	if ($result)
	{
	echo " Your order has been passed to admin for acceptance/rejection";
	}
	else
	{
	echo " failed";
	}
	?>

Recommended Answers

All 14 Replies

Change line 16 to this. If there's an error, you'll see it.

$result = mysql_query($query) or die(mysql_error);

it is giving the following error
"Database access failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order(customer_ID, product_id, quantity) VALUES ('1', 'PRAS1', '23')' at line 1"

but i think syntax is coorect

What are the database types for your fields. Try 1 and 23 without single quotes. Or perhaps product_id should be a number too.

customer_ID and quantity are of int type while product_id is of varchar(15) type

yes even then its not working

Then maybe the connection is wrong, try:

$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("pras2") or die(mysql_error());

same error... thee is one more thing i have 2 more column in my database tables "order"
one id order_id its auto_increment thats why i didnot assign it any value through php.
and other is status which i set default to be pending.

Please try the bellow connection script I think it will work

$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("pras2", $con) or die(mysql_error());

I see it now... overlooked. The table name is the issue. Order is a reserved word. Put backticks around them like so: `order`

i tried that but no use

yeah u r right now its working by placing backtick around order

posting my code if it helps other as well.

<?php 
session_start();
//print_r($_POST);
$name=$_SESSION['SESS_CUSTOMER_ID'];

$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("pras2", $con) or die(mysql_error());

	$product_id=$_POST['P_list'];
	echo "$product_id";
	$quantity=$_POST['quantity'];
	
	$query="INSERT INTO `order`(customer_ID, product_id, quantity) VALUES ($name, '$product_id', $quantity)";
//die($query);

	$result=mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
	else
	{
	echo " Your order has been passed to admin for acceptance/rejection";
	}
	?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.