0

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";
	}
	?>
3
Contributors
14
Replies
15
Views
5 Years
Discussion Span
Last Post by pritaeas
Featured Replies
  • yeah u r right now its working by placing backtick around order posting my code if it helps other as well. [CODE]<?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 … Read More

0

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

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

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

0

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

0

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

0

Then maybe the connection is wrong, try:

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

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.

0

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());

0

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

1

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";
	}
	?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.