Jump to content

Undefined Index Problem


andrewgarn

Recommended Posts

I had this mysql/php code working fine on my home apache server.

 

I uploaded it to a remote server and suddenly get undefined index errors.

 

Notice: Undefined index: cmd in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 25

Notice: Undefined index: email in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 27
Search for a Certificate
Certificate ID:
Select a Hull Number

Hullnumber - Certificate ID
14234 - 10


Notice: Undefined index: searchcert in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 67

Notice: Undefined index: delete in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 78

Notice: Undefined index: email in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 94

Notice: Undefined index: cmd in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 145

Notice: Undefined index: cmd in /.automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php on line 145

This occurs on every page, and as far as I can see the variables do exist.

 

Here is the code for this page:

 

<?
// logincheck
session_start();
if (empty($_SESSION['username'])) {
echo "There is a problem with your login";
include "index.html"; 
} 
else {

include "header.php";
?><div id="body"><?
//Database Information
$dbhost = "****";
$dbname = "****";
$dbuser = "****";
$dbpass = "****";

//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

//If cmd has not been initialized
if(!isset($cmd))
{
   if(!$_GET["cmd"]=="edit") {
   
   if (!$_POST["email"]) {
   ?>
   <h2>Search for a Certificate</h2>
   <!--<form action="editboat.php" method="post">
   Hull Number: <INPUT TYPE="TEXT" NAME="hullnumber" VALUE="" SIZE=10><br>
   <input type="submit" name="searchhull" value="Search" />
   </form> -->
   
   <form action="editboat.php" method="post">
   Certificate ID: <INPUT TYPE="TEXT" NAME="certid" VALUE="" SIZE=10>
   <input type="submit" name="searchcert" value="Search" />
   </form>
   <?
   //echo '<p>Select Mode</p>';
   //display all the boat information
   $result = mysql_query("SELECT * FROM boat ORDER BY hullnumber"); 
   
   //run the while loop that grabs all the boats
   //echo "<h2>Edit a Boat</h2>";
   ?><h2>Select a Hull Number</h2><?
   echo "<p>Hullnumber - Certificate ID</p>";
   while($r=mysql_fetch_array($result)) 
   { 
      //hull number and certid.
      $hullnumber=$r["hullnumber"];//take out the hullnumber
      $certid=$r["certid"];//take out the certid
  $ownerid=$r["ownerid"];//take out userid
      //echo $ownerid;
 //make the title a link
  echo "<a href='editboat.php?cmd=edit&certid=$certid'>$hullnumber - $certid</a>";
  //echo "<a href='editboat.php?cmd=delete&certid=$certid'> - Delete</a>";
      echo "<br>";
}
echo "<p></p>";
}
}
}
?>

<?
if ($_POST["searchcert"])
{
$certid = $_POST["certid"];
//echo $certid;
$pathdirectory = "editboat.php?cmd=edit&certid=$certid";
echo "<meta http-equiv=\"refresh\" content=\"1;url=$pathdirectory\" />";
    }
else {
?>

<?
if ($_POST["delete"])
{
	$hullnumber = $_POST["hullnumber"];
	//echo $hullnumber;
	$deletesql = "DELETE FROM boat where hullnumber='$hullnumber'";
	//echo $deletesql;
	$deleteresult = mysql_query($deletesql);
	echo "<p></p>";
	echo "Record Deleted!";
	?>
    	---<meta http-equiv="Refresh" content="1; URL=database.php">
    	<?
}
?>

<?
if ($_POST["email"])
{
	echo "<h2>Generating Email</h2>";
	$hullnumber = $_POST["hullnumber"];
	$certid = $_POST["certid"];
	$ownerid = $_POST["ownerid"];
	$design = $_POST["design"];
	$designer = $_POST["designer"];
	$dateofmeasurement = $_POST["dateofmeasurement"];
	$length = $_POST["length"];
	$weight = $_POST["weight"];
	$rigs = $_POST["rigs"];
	$draught = $_POST["draught"];
	$status = $_POST["status"];
	$registrar = $_POST["registrar"];
	$measurer = $_POST["measurer"];

	$query = "SELECT * FROM owner WHERE ownerid='$ownerid'";
	$result = mysql_query($query);
	$myrow = mysql_fetch_array($result);
	//echo $myrow["ownerid"];
	$forename = $myrow["forename"];
	$surname =  $myrow["surname"];
	$telephone = $myrow["telephone"];
	$email = $myrow["email"];
	$addressline1 = $myrow["addressline1"];
	$city =  $myrow["city"];
	$county = $myrow["county"];
	$postcode = $myrow["postcode"];

	$subject = 'Certificate for hullnumber: <b>'.$hullnumber.'</b> owned by: <b>'.$forename.' '.$surname.'</b>'; 
	//echo $subject;
	$random_hash = md5(date('r', time()));
	$headers .="From: ag605@ecs.soton.ac.uk\r\nReply-To: ag605@ecs.soton.ac.uk";
	$headers .="\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
	ob_start();
	?>
        --PHP-alt-<?php echo $random_hash; ?> 
	Content-Type: text/plain; charset="iso-8859-1"
	Content-Transfer-Encoding: 7bit
        Hello World
        <?
	$message = ob_get_clean();
	$mail_sent = @mail( $email, $subject, $message, $headers );
	echo $mail_sent ? "Mail sent" : "Mail failed";


}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      if (!isset($_POST["email"]))
  {
  ?><h2>Editing Record</h2><?
  //echo '<p>Edit Mode</p>';
  $certif = $_GET["certid"];
      //$owneridif = $_GET["ownerid"];
  $sql = "SELECT * FROM boat WHERE certid='$certif'";
      //echo $sql;
  $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
  $localownerid = $myrow["ownerid"];
  $sql2 = "SELECT forename, surname FROM owner WHERE ownerid='$localownerid'";
  $result2 = mysql_query($sql2);
  $myrow2 = mysql_fetch_array($result2); 	  	  	  	  
  ?>
  
      <form action="editboat.php" method="post">
      <input type=hidden name="certid" value="<?php echo $myrow["certid"] ?>">
   
      <b>Hull Number:</b> <INPUT TYPE="TEXT" NAME="hullnumber" VALUE="<?php echo $myrow["hullnumber"] ?>" SIZE=5>
      <b>Certificate Number:</b> <INPUT TYPE="TEXT" NAME="certid" VALUE="<?php echo $myrow["certid"] ?>" SIZE=5><br><br />
      <b>Owner's ID:</b> <INPUT TYPE="TEXT" NAME="ownerid" VALUE="<?php echo $myrow["ownerid"] ?>" SIZE=5> - (<?php echo $myrow2["forename"] ?> <?php echo $myrow2["surname"] ?>)<br><br />
      <b>Design:</b> <INPUT TYPE="TEXT" NAME="design" VALUE="<?php echo $myrow["design"] ?>" SIZE=10>
      <b>Designer:</b> <INPUT TYPE="TEXT" NAME="designer" VALUE="<?php echo $myrow["designer"] ?>" SIZE=15><br><br />
      <b>Measurement Date:</b> <INPUT TYPE="TEXT" NAME="dateofmeasurement" VALUE="<?php echo $myrow["dateofmeasurement"] ?>" SIZE=8><br>   <br />  
      <b>Length:</b> <INPUT TYPE="TEXT" NAME="length" VALUE="<?php echo $myrow["length"] ?>" SIZE=5>
      <b>Weight:</b> <INPUT TYPE="TEXT" NAME="weight" VALUE="<?php echo $myrow["weight"] ?>" SIZE=5><br><br />
      <b>Rigs:</b> <INPUT TYPE="TEXT" NAME="rigs" VALUE="<?php echo $myrow["rigs"] ?>" SIZE=5>
      <b>Draught:</b> <INPUT TYPE="TEXT" NAME="draught" VALUE="<?php echo $myrow["draught"] ?>" SIZE=5><br><br />
      <b>Status:</b> <INPUT TYPE="TEXT" NAME="certstatus" VALUE="<?php echo $myrow["certstatus"] ?>" SIZE=5><br><br />
      <b>Registrar:</b> <INPUT TYPE="TEXT" NAME="registrar" VALUE="<? echo $myrow["registrar"] ?>" SIZE=8>
      <b>Measurer:</b> <INPUT TYPE="TEXT" NAME="measurer" VALUE="<?php echo $myrow["measurer"] ?>" SIZE=8><br><br />
   
      <input type="hidden" name="cmd" value="edit">
      <p></p>
      <input type="submit" name="submit" value="Update Boat Record"> or 
      <input type="submit" name="email" value="Email Certificate" /> or 
      <input type="submit" name="delete" value="Delete" /> 
  </form>
      <!-- <form method="link" action="database.php">
      <input type="submit" name="cancel" value="Cancel" />
      </form> -->
      
      <?
  //display owner names with their id's
  //echo "<h2>Owner Names and ID's</h2>";
  //$listresults = mysql_query("SELECT * FROM owner"); 
   
   	  //run the while loop that grabs all the news scripts
      //while($r=mysql_fetch_array($listresults)) 
      //{ 
      	//hull number and certid.
        //$ownerid=$r["ownerid"];//take out the ownerid
        //$forename=$r["forename"];//take out the forename
    //$surname=$r["surname"];//take out the surname
     
    //make the title a link
    //echo "$forename $surname ($ownerid)";
        //echo "<br>";
  //}
    //echo "<p></p>";
  //?>
   
   <? } } ?>
   
   
   <?
   if ($_POST["submit"])
   {
      //echo '<p>Submit Mode</p>';
  $hullnumber = $_POST["hullnumber"];
      $certid = $_POST["certid"];
      $ownerid = $_POST["ownerid"];
  $design = $_POST["design"];
  $designer = $_POST["designer"];
  $dateofmeasurement = $_POST["dateofmeasurement"];
  $rigs = $_POST["rigs"];
  $length = $_POST["length"];
  $weight = $_POST["weight"];
  $draught = $_POST["draught"];
  $certstatus = $_POST["certstatus"];
  $registrar = $_POST["registrar"];
  $measurer = $_POST["measurer"];
  	  
  //echo $sql;

      $sql = "UPDATE boat SET hullnumber='$hullnumber',certid='$certid',ownerid='$ownerid', design='$design', designer='$designer', dateofmeasurement='$dateofmeasurement', rigs='$rigs', length='$length', weight='$weight', draught='$draught', certstatus='$certstatus', registrar='$registrar', measurer='$measurer' WHERE certid='$certid'";

//insert into boat (hullnumber, certid, ownerid, design, designer, dateofmeasurement, rigs, length, weight, draught, certstatus, registrar, measurer) values ('2', '2', '1', 'motor', 'Daivdson', '2008-01-12', '2', '25', '15', '40', 'Yes', 'ag605', 'tw106')

      $result = mysql_query($sql) or die(mysql_error());
      echo "Thank you! Information updated.";
  echo "<p><a href='database.php'>Return to Administration</a></p>";
   }
}
}
}

?>
</div>
<div id="footer">
</div>
</body>
</html>

 

Anyone got an idea of why it isnt working? I know the server works fine, as friends are using it with no problem.

Link to comment
Share on other sites

Kind of hard to explain here but looks like your script is still looking for the files in the original directory that they would have been on when they where on the old server e.g

 

automount/tiree/staffpg/ughome/ag605/public_html/info2007/editboat.php

 

instead of :

 

//editboat.php

 

Does that make sence ?

 

 

 

Link to comment
Share on other sites

The problem is to do with how you are checking user defined variables ($_POST, $_GET, $_COOKIE).

 

You should check to see if a user defined variable exists first before using it, eg:

 

if(isset($_GET['some_var']) && !empty($_GET['some_var']))

The following examples is not the same:

if(!empty($_GET['some_var']))

// OR

if(!$_GET['some_var'])

The first example tells PHP to see if the variable exists first before checking to see if it is emtpy. If you don't tell PHP to check the variable existence first PHP will assume the variable exists and tries to do what you're telling it to do. But PHP can't and returns a notice message.

Link to comment
Share on other sites

In current versions of PHP, notices are turned off by default. The proper way to do it, is check for the keys existence first with array_key_exists() or isset(). But, I find this to be a pain and usually just turn NOTICE reporting off.

Link to comment
Share on other sites

Oh so basically my code is seeing if a variable is empty but when the variable isnt there its throwing errors?

 

So i'll just change them to isset and it will work fine?

 

Thanks, I guess the server must be using an older version of php.

 

Cheers, i'll post if the fix worked on it in a few minutes

Link to comment
Share on other sites

The point here is that your code is referencing Array Indexes in associative arrays.  In other words, you have places where you are trying to reference a value in an array, say $_POST['name'], when in fact there is no array member that exists for the index 'name'.  PHP is weakly typed, and part of the advantage of using it is that you don't have to worry about these types of details as it's not an error, or a problem, depending on what your script is doing.

 

You are seeing these messages because your php.ini file has this Error level set on.  Notices are informational, and on a production server you should have them off anyways, or at least logged into a file.  This setting really exists for debugging, and for those who insist on not having any unhandled code, but it's safe enough to turn down your settings.

Link to comment
Share on other sites

How would i remove the errors from this?

 

<?php $self = $_SERVER['PHP_SELF'];

//$ownerid = $_POST['ownerid'];

$forename = $_POST['forename'];

$surname = $_POST['surname'];

$telephone = $_POST['telephone'];

$email = $_POST['email'];

$addressline1 = $_POST['addressline1'];

$city = $_POST['city'];

$county = $_POST['county'];

$postcode = $_POST['postcode'];

Link to comment
Share on other sites

I presume you use a submit button to submit the form. If you name the submit you can use:

<?php 
$self = $_SERVER['PHP_SELF'];
// $_POST['[submit'] is the name of your forms' submit button
if(isset($_POST['submit']))
{
         //$ownerid =   $_POST['ownerid'];
         $forename = $_POST['forename'];
         $surname = $_POST['surname'];
         $telephone = $_POST['telephone'];
         $email = $_POST['email'];
         $addressline1 = $_POST['addressline1'];
         $city =   $_POST['city'];
         $county =   $_POST['county'];
         $postcode =   $_POST['postcode'];

         // rest of your code
}

?>

Link to comment
Share on other sites

How will that affect the form below it?

 

Surely it will just throw errors in the form because those variables are not defined? Form submits using those values.

 

<?php		$self = $_SERVER['PHP_SELF'];
		//$ownerid =	$_POST['ownerid'];
		$forename = $_POST['forename'];
		$surname = $_POST['surname'];
		$telephone = $_POST['telephone'];
		$email = $_POST['email'];
		$addressline1 = $_POST['addressline1'];
		$city =	$_POST['city'];
		$county =	$_POST['county'];
		$postcode =	$_POST['postcode'];

#html form
$form = "<form action=\"$self\" method=\"post\">";
//$form.= "Owner ID: <input type=\"text\" name=\"ownerid\" ";
//$form.= "size=\"20\" value=\"$ownerid\"> <br>";
$form.= "<b>Forename:</b> <input type=\"text\" name=\"forename\" ";
$form.= "size=\"20\" value=\"$forename\">";
$form.= "<b> Surname:</b> <input type=\"text\" name=\"surname\" ";
$form.= "size=\"20\" value=\"$surname\"> <br><br>";
$form.= "<b>Telephone:</b> <input type=\"text\" name=\"telephone\" ";
$form.= "size=\"20\" value=\"$telephone\"> <br><br>";
$form.= "<b>Email:</b> <input type=\"text\" name=\"email\" ";
$form.= "size=\"20\" value=\"$email\"> <br><br>";
$form.= "<b>Address:</b> <input type=\"text\" name=\"addressline1\" ";
$form.= "size=\"20\" value=\"$addressline1\"> <br><br>";
$form.= "<b>City:</b> <input type=\"text\" name=\"city\" ";
$form.= "size=\"20\" value=\"$city\"> <br><br>";
$form.= "<b>County:</b> <input type=\"text\" name=\"county\" ";
$form.= "size=\"20\" value=\"$county\"> <br><br>";
$form.= "<b>Postcode:</b> <input type=\"text\" name=\"postcode\" ";
$form.= "size=\"20\" value=\"$postcode\"> <br><br>";
$form.= "<input type=\"submit\" name=\"submit\" ";
$form.= "value=\"Add Owner\"> </form>";	

Link to comment
Share on other sites

If your form contains text fields your browser will include these fields in the _POST data whether their value is empty or not.

 

Fields in which you should check to see if they exists in the _POST data is checkbox, select and file fields.

 

You should always check to see if $_GET data exists first before using it. The same applies to COOKIE too.

Link to comment
Share on other sites

I cant change the ini file in the remote server I need to host it on, so I need to find how to remove the errors.

 

Would i have to put?

 

if(isset($_POST['submit'])) { $variable } in the form?

 

e.g.

 

$form.= "<b>Forename:</b> <input type=\"text\" name=\"forename\" ";

$form.= "size=\"10\" value=\"if(isset($_POST['submit'])) { $forename }\">";

Link to comment
Share on other sites

You can't place a condition within a string. You only need to make sure $_POST['submit'] exists only once. You don't do it for all your form variables:

<?php
// being lazy 
$forename = $surname = $telephone = $email = $addressline1 = $city = $county = $postcode = null;

if(isset($_POST['submit']))
{
    //$ownerid    =	$_POST['ownerid'];
    $forename     = $_POST['forename'];
    $surname      = $_POST['surname'];
    $telephone    = $_POST['telephone'];
    $email        = $_POST['email'];
    $addressline1 = $_POST['addressline1'];
    $city         = $_POST['city'];
    $county       = $_POST['county'];
    $postcode     = $_POST['postcode'];
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <p><b>Forename:</b> <input type="text" name="forename" size="20" value="<?php echo $forename; ?>"></p>
  <p><b>Surname:</b> <input type="text" name="surname" size="20" value="<?php echo $surname; ?>"></p>
  <p><b>Telephone:</b> <input type="text" name="telephone" size="20" value="<?php echo $telephone; ?>"></p>
  <p><b>Email:</b> <input type="text" name="email" size="20" value="<?php echo $email; ?>"></p>
  <p><b>Address:</b> <input type="text" name="addressline1" size="20" value="<?php echo $addressline1; ?>"></p>
  <p><b>City:</b> <input type="text" name="city" size="20" value="<?php echo $city; ?>"></p>
  <p><b>County:</b> <input type="text" name="county" size="20" value="<?php echo $county; ?>"></p>
  <p><b>Postcode:</b> <input type="text" name="postcode" size="20" value="<?php echo $postcode; ?>"></p>
  <input type="submit" name="submit" value="Add Owner">
</form>

Link to comment
Share on other sites

I was working on making it work by duplicating the form into the opposite

 

if(!isset($_POST['submit'])) {

 

then repeating the form without the variables, but your method looks better with less repitition.

 

Thanks, i'll work on that now, sorry to bother with questions.

Link to comment
Share on other sites

  • 2 weeks later...

I cant change the ini file in the remote server I need to host it on, so I need to find how to remove the errors.

 

Would i have to put?

 

if(isset($_POST['submit'])) { $variable } in the form?

 

e.g.

 

$form.= "Forename:

$form.= "size=\"10\" value=\"if(isset($_POST['submit'])) { $forename }\">";

 

 

Use this at the top of your script to set the reporting:  http://us2.php.net/manual/en/function.error-reporting.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.