hai guyz...1st of all i would lik 2 thank to those who teach me on how to do an announcement...now i got new problem...im doin online food delivering system within campus...so user can view the food and if the user click buy, they can view the name, price n description of the food. So the user need to key in their email,apartment name , room number and quantity. so it wil store into database and the admin can view the name, price n description of the food plus user email,apartment name , room number and quantity ...the problem now is after i key in everthing and click buy it show that the order is successfuly sent but the db is empty.I hope somebody can solve my problem....below is my script....10q...

<?php

/* fetch data from form input */

extract ($_POST);

/* connect to server */

$db = mysql_connect("localhost", "root", "") or die ("Could Not Connect to Server" . mysql_error());

/* select database */

mysql_select_db("webauth", $db) or die ("Could Not Find Database" . mysql_error());


/* fields' names in the database */
$result = "INSERT INTO buy (id,type,price,desc,email,apartment,roomno,qty)";

   /* variable name as declared as above to be entered to the field */
$result .= "VALUES ('$id', '$type', '$price','$desc','$email','$apartment','$roomno','$qty')";

mysql_query($result, $db);


/* variable name as declared as above to be entered to the field */
/* fields' names in the database */
/* execute the query */

echo "successful";
echo "<br>";
echo "<a href=order.php>Home</a>";
mysql_close($db);


?>

You should start trying to use transactions, that way if there was an error, any changes that successfully went through are also rolled back.

<?php
$debug = true;
//variable to commit transaction
$commit = "commit";
$errors = array();

/* fetch data from form input */

extract ($_POST);

/* connect to server */

if(!$db = mysql_connect("localhost", "root", ""))
{
	$errors[] = mysql_error();
}

/* select database */

if(!mysql_select_db("webauth", $db))
{
	$errors[] = mysql_error();
}

//begin transaction
if(!mysql_query("begin"))
{
	$errors[] = mysql_error();
}

/* fields' names in the database */
$result = "INSERT INTO buy (id,type,price,desc,email,apartment,roomno,qty)";

/* variable name as declared as above to be entered to the field */
$result .= "VALUES ('$id', '$type', '$price','$desc','$email','$apartment','$roomno','$qty')";

if(!mysql_query($result, $db))
{
	$commit = "rollback";
	$errors[] = mysql_error();
}


//$commit = "rollback";  //uncomment to automatically rollback the transaction

if($commit == "commit")
{
	mysql_query($commit);
	echo "successful";
	echo "<br>";
	echo "<a href=order.php>Home</a>";
	mysql_close($db);
}
else
{
	echo "there was an error in your transaction, transction rolled back: ";
	if($debug == true)
	{
		foreach($errors as $value)
		{
			echo "<p>" . $value . "</p>";
		}
	}
	mysql_query($commit);
	mysql_close($db);
}
/* variable name as declared as above to be entered to the field */
/* fields' names in the database */
/* execute the query */


?>

10x for the code Mrr.Robbob....but there is a syntax error in the code...if i delete id,type,price n desc...the code works wel...for ur information the id,type,price n desc wil display automatically wen user click the food picture...the user only keyin the email,roomno,apartment and quantity...but the db mus show id,type,price,desc,email,roomno,apartment n quantity....can u help me wit these Mr.Robbob...

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.