Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Notice: Undefined index

Posted on 2009-04-23
2
Medium Priority
?
918 Views
Last Modified: 2013-12-13
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
Comment
Question by:qualityip
2 Comments
 
LVL 19

Accepted Solution

by:
Michael701 earned 1000 total points
ID: 24217746
change

if($_POST["request"])


to

if(isset($_POST["request"]))
0
 
LVL 13

Assisted Solution

by:marchent
marchent earned 1000 total points
ID: 24217747
if($_POST["request"])


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

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
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.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

571 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