• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 927
  • Last Modified:

Notice: Undefined index

Hey,

I am working on a simple PHP program. It connects to a DB to pull a list of products with a check box next to each product for a form that submits with customer information. The page seems to work perfectly as I can submit my form and receieve confirmation but I am receiving an error in the middle of the page:

Notice: Undefined index: request in C:\wamp\www\index.php on line 10

PLEASE HELP! The code is below.
<?php
 
//database variables
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_database = "business";
 
//is this a purchase request
if($_POST["request"])
{
	//error checking
	$errors = array();
 
	//check first name
	if(strlen($_POST["firstName"]) <= 0)
	{
		$errors[] = "You must enter a first name!";
	}
 
	//check last name
	if(strlen($_POST["lastName"]) <= 0)
	{
		$errors[] = "You must enter a last name!";
	}
 
	//check email
	if(strlen($_POST["emailAddress"]) <= 0)
	{
		$errors[] = "You must enter an email address!";
	}
 
	//were there errors
	if(count($errors) > 0)
	{
		echo "<font color=\"red\">There were errors!<ul>";
 
		foreach($errors as $error)
		{
			echo "<li>" . $error . "</li>";
		}
 
		echo "</ul></font>";
	}
	else
	{
		//no errors, determine what products were bought
		$purchases = array();
 
		foreach($_POST as $p_key => $p_val)
		{
			//is this a checkbox and is it checked
			if(substr($p_key, 0, 3) == "buy" && $p_val == "on")
			{
				$purchases[] = intval(substr($p_key, 3));
			}
		}
 
		//open database connection
		$con = mysql_connect($db_host, $db_user, $db_pass);
		mysql_select_db($db_database, $con);
 
		//does the account already exist
		$query = mysql_query("SELECT * FROM `customer` WHERE `firstName` = '" . $_POST["firstName"] . "' AND `lastName` = '" . $_POST["lastName"] . "'", $con);
 
		if(mysql_num_rows($query) <= 0)
		{
			//we need to create the user's account
			mysql_query("INSERT INTO `customer` VALUES('" . $_POST["firstName"] . "', '" . $_POST["lastName"] . "', '" . $_POST["emailAddress"] . "', 0)", $con);
		}
 
		//update itemsPurchased table
		foreach($purchases as $p)
		{
			//sql statement
			$sql = "INSERT INTO `itemspurchased` VALUES('" . $_POST["emailAddress"] . "', " . $p . ")";
 
			//query database
			mysql_query($sql, $con);
		}
 
		//print out status
		echo "Order successful!<br />";
		echo "First Name: " . $_POST["firstName"] . "<br />";
		echo "Last Name: " . $_POST["lastName"] . "<br />";
		echo "Email: " . $_POST["emailAddress"] . "<br />";
		echo "<br />";
 
		//close db
		mysql_close($con);
	}
}
 
?>
<form method="post" action="index.php">
<input type="hidden" name="request" value="yes" />
<table border="0">
<tr><td><b>First Name:</b></td><td><input type="text" name="firstName" /></td></tr>
<tr><td><b>Last Name:</b></td><td><input type="text" name="lastName" /></td></tr>
<tr><td><b>Email:</b></td><td><input type="text" name="emailAddress" /></td></tr>
</table>
<br />
<table border="1">
<tr><td>Buy</td><td>Name</td><td>Price</td></tr>
<?php
 
//retrieve products and display them
$con = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_database, $con);
$query = mysql_query("SELECT * FROM `product`", $con);
 
while($result = mysql_fetch_array($query))
{
	echo "<tr>";
	echo "<td><input type=\"checkbox\" name=\"buy" . $result["productID"] . "\" /></td>";
	echo "<td>" . $result["name"] . "</td>";
	echo "<td>" . $result["price"] . "</td>";
	echo "</tr>";
}
 
mysql_close($con);
 
?>
</table><br />
<input type="submit" value="Purchase" />
</form>

Open in new window

0
qualityip
Asked:
qualityip
2 Solutions
 
Michael701Commented:
change

if($_POST["request"])


to

if(isset($_POST["request"]))
0
 
marchentCommented:
if($_POST["request"])


if(isset($_POST["request"]))

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now