Notice: Undefined index

Posted on 2009-04-23
Last Modified: 2013-12-13

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.

//database variables

$db_host = "localhost";

$db_user = "root";

$db_pass = "";

$db_database = "business";

//is this a purchase 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>";




		//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





<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>


<br />

<table border="1">



//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>";




</table><br />

<input type="submit" value="Purchase" />


Open in new window

Question by:qualityip
    LVL 19

    Accepted Solution




    LVL 13

    Assisted Solution




    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now