Page 1 of 1

[SOLVED] Undefined variable - but i think it is!

Posted: Wed Sep 07, 2005 10:07 am
by tom.cull
i am creating a page that will display a record in an html field based on a id from a url string.

i have got this code:

Code: Select all

<html>
<body>


<?php
error_reporting(E_ALL);

$username="xxxx_xxxx";
$password="xxxx";
$database="xxxx_xxxx";

  // editing/adding script

  // get the id from the URL request
  $id = $_REQUEST['id'];  

  if( $id )
  {  
     // connect to the server
     mysql_connect( 'localhost', $username, $password )
        or die( "Error! Could not connect to database: " . mysql_error() );
 
     // select the database
     mysql_select_db( $database )
       or die( "Error! Could not select the database: " . mysql_error() );
   
     // retrieve the row from the database
     $query = "SELECT * FROM used_cars WHERE id='$id'";
 
     $result = mysql_query( $query );

     if( $result && $contact = mysql_fetch_object( $result ) )
     {
        // set our variables
        $contact_name = $contact -> contact_name;
        $contact_email = $contact -> contact_email;
        $contact_phone = $contact -> contact_phone;
  $car_make = $contact -> car_make;
        $car_model = $contact -> car_model;
        $car_year = $contact -> car_year;
  $car_mileage = $contact -> car_mileage;
        $car_price = $contact -> car_price;
        $car_type = $contact -> car_type;
        $car_info = $contact -> car_info;
  $car_pbracket = $contact -> car_pbracket;  
     }
  }  
   
  // print out the form
  ?>        
  <form action="saveitem.php" method="get">
  <input type="hidden" name="id" value="<?php echo($id) ?>">

 
 <table border="1" cellpadding="3" cellspacing="3">
   <tr>
     <th align="left">Contact Name</th>
     <td align="left"> <input name="contact_name" type="text" value="<?php echo $contact_name ?>"></td>
   </tr>
   <tr>
     <th align="left">Contact Email</th>
     <td align="left"> <input name="lastName" type="text" value="<?php echo($contact_email) ?>" />
     </td>
   </tr>
   <tr>
     <th align="left">Contact Phone</th>
     <td align="left"> <input name="email" type="text" value="<?php echo($contact_phone) ?>" />
     </td>
   </tr>
   <tr>
     <th align="left">Car Make</th>
     <td align="left"><input name="email2" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Model</th>
     <td align="left"><input name="email3" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Year</th>
     <td align="left"><input name="email4" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Mileage</th>
     <td align="left"><input name="email5" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Price</th>
     <td align="left"><input name="email6" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Type</th>
     <td align="left"><input name="email7" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Info</th>
     <td align="left"><input name="email8" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
   <tr>
     <th align="left">Car Price Bracket</th>
     <td align="left"><input name="email9" type="text" value="<?php echo($contact_phone) ?>" /></td>
   </tr>
 </table>
 
  <br>
  <input type="submit" value="Save Entry">
  <br>
  <a href="delete.php?id=<?php echo($id) ?>">Delete Entry</a>
     
  </form>    
 
</body>
</html>
when I run the file I get the table with nothing but

<br> in each field. When I views the source of the page created, I got:

<td align="left"> <input name="contact_name" type="text" value="<br />
<b>Notice</b>: Undefined variable: contact_name in <b>/xxxx/xxxx/xxxx_xx/xxx/editdetails.php</b> on line <b>58</b><br />
"></td>

Code: Select all

for each cell of the HTML table.

It is obviously telling me that there the variable is not set, however I thought that i have done that in the initall php code above?

can any one shed any light?

thanks

Posted: Wed Sep 07, 2005 11:02 am
by feyd

Code: Select all

if( $result && $contact = mysql_fetch_object( $result ) )
either of the two sub-expressions in the if clause evaluating to false, zero, null, empty-string, etc... will result in such an error, along with $id evaluating to any of those as well.

Posted: Wed Sep 07, 2005 12:12 pm
by tom.cull
thanks for that - i will have a look - im new to this so not sure what you mean but i'll find out!!

Posted: Wed Sep 07, 2005 12:33 pm
by tom.cull
hi - thanks for your help - the problem is now ressolved- rather embarasinly, i named the table wrong. :oops: