can anyone help me out with these errors i'm getting please?
and if they can explain why im getting them and how i can avoid them in future
thanks :)

Notice: Undefined index: prod_name in C:\wamp\www\uniwork\update_database.php on line 17

Notice: Undefined index: prod_price in C:\wamp\www\uniwork\update_database.php on line 24

Notice: Undefined index: prod_desc in C:\wamp\www\uniwork\update_database.php on line 31

Notice: Undefined index: prod_colour in C:\wamp\www\uniwork\update_database.php on line 38

<!DOCTYPE HTML PUBLIC  
               "-//W3C//DTD HTML 4.0 Transitional//EN" 
               "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
  <title>Add an Animal with Validation</title> 
</head> 
<body> 
<h2>The action script for adding animal with validation </h2> 
<p>Look at the <a href="addAnimalwithValid.phps">source code</a> and notice there is some validation and cleaning of the data. </p> 
 <?php 
  
      // Create a string to collect and report back any errors 
     $errorString = ""; 
     //See if any info was submitted 
     $prodname = $_GET['prod_name'];  
     //Clean data - trim space 
     $prodname = trim ( $prodname);  
     //Check its ok - if not then add an error message to the error string 
     if (empty($prod_id))  
       $errorString = $errorString."<br>Please supply a product name."; 
	         
     $prodprice = $_GET['prod_price'];    
     //Clean data - trim space 
     $prodprice = trim ( $prodprice); 
     //Check its ok - if not then add an error message to the error string 
     if (empty($prodprice))  
       $errorString = $errorString."<br>Please supply a product price in the £xxx.xx format.";
	      
     $proddesc = $_GET['prod_desc'];    
     //Clean data - trim space 
     $proddesc = trim ( $proddesc); 
     //Check its ok - if not then add an error message to the error string 
     if (empty($proddesc))  
       $errorString = $errorString."<br>Please supply a product description."; 
	   
	        $prodcolour = $_GET['prod_colour'];    
     //Clean data - trim space 
     $prodcolour = trim ( $prodcolour); 
     //Check its ok - if not then add an error message to the error string 
     if (empty($prodcolour))  
       $errorString = $errorString."<br>Please supply the dominant product colour.";     
   // check if there were any errors 
    if (!empty($errorString)) 
    { 
          //If there were, print the error string and exit 
        print "<b>There were errors</b>.<br>".$errorString; 
       
    } 
    else  //There wer No errors. Insert the data 
    { 
     include "include/connect.php"; 
 $query = "INSERT INTO symbols (prod_name, prod_price, prod_desc, prod_colour) VALUES ('$prodname', '$prodprice', '$proddesc','$prodcolour')";   
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());  
     //This function returns the id of the new row in the table 
    $id = mysql_insert_id(); 
   // print message with ID of inserted record  
    echo "<br />New record inserted with ID = $id";  
     
    //Create a new query to display the new row in a table 
    $query = "Select * from symbols where id = '$id'"; 
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());  
    echo "<table cellpadding=10 border=1>";   
    while($row = mysql_fetch_array($result)) {  
        echo "<tr>";  
        echo "<td>".$row['prod_name']."</td>";  
        echo "<td>".$row['prod_price']."</td>";  
        echo "<td>".$row['prod_proddesc']."</td>";  
        echo "<td>".$row['prod_colour']."</td>";  
        echo "</tr>";  
    } //End while 
    echo "</table>";  
  }// End else - there where no errors  
?> 
</p> 
</body> 
</html>

Recommended Answers

All 4 Replies

Line 16 of your posted code. You're referencing $_GET. However, it appears $_GET doesn't have an element with the key 'prod_name'.

i'm not sure i quite understand
if you could explain in slightingly more simpler term
been learning the language now as part of my course for a few weeks so its not fully burnt it yet :P

Ok.

First off, $_GET is a superglobal used to access variables passed via GET. Traditionally, this is done through the URL, so index.php?myvar=1&yourvar=2 would result in $_GET looking like:

[myvar]=>1,
[yourvar=>2

This line:

$prodname = $_GET['prod_name'];

is asking PHP, "Access the $_GET array & look for the element identified with they key 'prod_name'. Take the associated value for that and put it in $prodname".

So PHP does that. It accesses $_GET, and starts looking for the 'prod_name' key. When it doesn't find one, it outputs that notice you're getting.

Either ensure the page is always called with a prod_name variable (in the same way my example above is called with 'myvar' and 'yourvar'), or preferably, check for the existence of prod_name before trying to access it. BTW - this advice also applies to the other 3 indices that weren't found.

$prodname = 'Default name';
if(isset($_GET['prod_name']))
{
  $prodname = $_GET['prod_name'];
}

>>if(!isset($_GET))
don’t trim before you know it is has content..
and a tip .. use brackets ... makes your code more readable even by you

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.