Jump to content

Undefined Index hekp


s_ainley87

Recommended Posts

Hello, I am trying to make an administration page for a website that will allow the user to add a product to a category and if it is a new category then to add a new category also,  however when i run the page in the browser i get an error that there is an undefined resource on Line 88 and the variable is a variable called a.  Now i am relativly new to PHP but i that these error often occur when POST and GET are being used incorrectly however i am using POST in the form and also the $_POST command so i dont think it could be that, i am posting the code so that if any one can see the error they can point me the right direction as i am aboslutely stuck,

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Jetstore | Adminstration</title>
</head>

<body>
<?php
// This page allows the administrator to add a product.

require_once ('include/mysql_connect.php'); // Connect to the database.
require_once ('include/config.inc.php');
if (isset($_POST['submitted'])) { // Check if the form has been submitted.

// Validate the product_name, image, category  size, price, and description.

// Check for a print name.
if (!empty($_POST['product_name'])) {
	$pn = escape_data($_POST['product_name']);
} else {
	$pn = FALSE;
	echo '<p><font color="red">Please enter the product\'s name!</font></p>';
}

// Check for an image.
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
	if (move_uploaded_file($_FILES['image']['tmp_name'], "uploads/{$_FILES['image']['name']}")) { // Move the file over.

		echo '<p>The file has been uploaded!</p>';

	} else { // Couldn't move the file over.
		echo '<p><font color="red">The file could not be moved.</font></p>';
		$i = FALSE;
	}
	$i = $_FILES['image']['name'];
} else {
	$i = FALSE;
}


// Check for a price.
if (is_numeric($_POST['price'])) {
	$p = (float) $_POST['price'];
} else {
	$p = FALSE;
	echo '<p><font color="red">Please enter the product\'s price!</font></p>';
}

// Check for a description
if (!empty($_POST['description'])) {
	$d = escape_data($_POST['description']);
} else {
	$d = '<i>No description available.</i>';
}

// Validate the category.
if ($_POST['category'] == 'new') {

	// If it's a new category, add the category to the database.
	$query = 'INSERT INTO category (category_name, category_desc) VALUES (';		

	if (!empty($_POST['category'])) {
		$query .= "'" . escape_data($_POST['category']) . "', ";
	} else {
		$query .= 'NULL, ';
	}


	// Check for a description.
	if (!empty($_POST['category_desc'])) {
		$query .= "'" . escape_data($_POST['category_desc']) . "')";

		// Improved MySQL Version:
		$result = mysql_query($query,$dbc);
		$a = mysql_insert_id($dbc);


		//$result = mysql_query ($query or die (mysql_error)); // Run the query.
		//$a = mysql_insert_id(); // Get the category ID.


	} else { 
				$a = FALSE;
		echo '<p><font color="red">Please enter the category\'s description!</font></p>';
	}

} elseif (($_POST["category"] = "existing") && ($_POST['existing'] > 0)) { // Existing category.
	$a = (int) $_POST['existing'];
} else { // No category selected.
	$a = FALSE;
	echo '<p><font color="red">Please enter or select the products\'s category!</font></p>';
}

if ($pn && $p && $a && $i) { // If everything's OK.

	// Add the print to the database.
	$query = "INSERT INTO product (product_name, category_id, price, product_desc, image_name) VALUES ('$pn', '$a', $p,'$d', '$i')"; echo $query;
	if ($result = $query) { // Worked.
	echo $result;
		echo '<p>The product has been added.</p>';
	} else { // If the query did not run OK.
		echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>'; 
	}

} else { // Failed a test.
		echo '<p><font color="red">Please click "back" and try again.</font></p>';
}

} else { // Display the form.
?>

<form enctype="multipart/form-data" action="admin.php" method="post">

	<input type="hidden" name="MAX_FILE_SIZE" value="524288">

	<fieldset><legend>Fill out the form to add a print to the catalog:</legend>

	<p><b>Product Name:</b> 
	  <input type="text" name="product_name" size="30" maxlength="60" /></p>

	<p><b>Image:</b> <input type="file" name="image" /> <small>The file name should not include spaces or other invalid characters and should have a file extension.</small></p>

	<p><b>Category:</b> 
<p><input name="category" type="radio" value="existing" />
Existing =>
<?php 		 


	$query="SELECT category_id,category_name FROM category";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=\"category\">Category Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[category_id]>$nt[category_name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
	?>

	<p>
	<input type="radio" name="category" value="new" /> New =>
	Category Name: <input type="text" name="category" size="25"/>
	<br /><br />
	Category Description:<textarea name="category_desc" cols="40" rows="5"></textarea></p>


	<p>Price: <input type="text" name="price" size="10" maxlength="10" />
	  <small>Do not include the currency sign or commas.</small></p>

	<p>Product Description:<textarea name="description" cols="40" rows="5"></textarea></p

	></fieldset>

	<div align="center"><input type="submit" name="submit" value="Submit" /></div>
	<input type="hidden" name="submitted" value="TRUE" />

</form>
<?php

} // End of main conditional.
?>
</body>
</html>

 

Thankyou very much in advance s_ainley87

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Jetstore | Adminstration</title>
</head>

<body>
<?php
// This page allows the administrator to add a product.

require_once ('include/mysql_connect.php'); // Connect to the database.
require_once ('include/config.inc.php');
if (isset($_POST['submitted'])) { // Check if the form has been submitted.

// Validate the product_name, image, category  size, price, and description.
        $product_name = isset($_POST['product_name'])?$_POST['product_name']:"";
// Check for a print name.
if (!empty($product_name)) {

 

That will remove any errors, you would have to do that for each post variable to ensure that it was set at one point, if not set it to nothing essentially.

Link to comment
Share on other sites

Yea. Basically, if you try to check an unset variable or array value (array['existing'], in this case) it will throw an error. Put an isset(array['keyname']) check before you check the value against anything else to make sure it exists first. In my honest opinion it should just fail equality checks if it isn't set (i.e. the value would be 'null' for any unset variables) but eh, I'm didn't write PHP:)

 

Cheers, message me if you still need help.

Link to comment
Share on other sites

for the most part "undefined" index isn't something you should have to worry about, in general its a warning, and normal php configs have it turned off to warn.

you can get by most of the post errors by saying

<?php
if(!empty($_POST)){
#Process post data
}
?>

 

because you defined the indexes in post via your html form any $_POST keys you say should be valid cause you made them

Link to comment
Share on other sites

Thankyou so much for all your help but it is still not working i have tried isset and all that jazz.

 

All i am trying to do is allow the user to add product and the user has a choice as to whether that product has a new category or belongs to an exsisting category (the SELECT menu) and then they can cat description and the price and and also an image and a product description is my code going about it the hard way?  If it is what would you suggest?

 

Please bare with me i am really new to php.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.