0

Scenario:

The user inputs the reference number and based on his reference number, I should display the location equivalent to it.

SQL:

require_once('conn.php');
$refnum = (isset($_POST['refOff'])) ; //Get filename set in form

$query = mysql_query("SELECT * FROM pilot WHERE geo=$refnum");
// display query results
while($row = mysql_fetch_array($query))
    {
    $rname =$row['rname'];
    $pname =$row['pname'];
    $mname =$row['mname'];                           
} 

HTML:

    <tr>
        <td width="283" height="32">Region:</span> </td>
        <td width="407"> <input type="text"value="<?php echo $rname;?>"/></td>
    </tr>
    <tr>
        <td width="283" height="32">Province:</span> </td>
        <td width="407"> <input type="text"value="<?php echo $pname;?>"/></td>
    </tr>
   <tr>
        <td width="283" height="32">City:</span> </td>
        <td width="407"> <input type="text"value="<?php echo $mname;?>"/></td>
    </tr>

The PROBLEM:

Errors are being displayed saying the rname,pname,and mname are undefined. What is wrong?Thanks again

LadyWinter
  • 307
  • 6
  • 13
  • Is all of this in the same file? – Darren Aug 01 '14 at 03:03
  • 4
    If you say `$refnum = (isset($_POST['refOff']))`, then wouldn't `$refnum` always be a boolean (true/false)? In that case, your query will probably always return zero rows, and your while loop would not be run. --> Thus leading to your variables ($rname, $pname, and $mname) not being set. I would recommend to always prepare/execute queries, but in this case, you could just say `$refnum = intval(isset($_POST['refOff'])?$_POST['refOff']:0);`. – Dave Chen Aug 01 '14 at 03:03
  • None of your inputs have a name attribute -> ie. `` should be `` – Sean Aug 01 '14 at 03:06
  • 2
    and use `mysqli` or `PDO` instead – Kevin Aug 01 '14 at 03:06
  • to piggyback on Dave Chen's point, if the query yielded 0 results then you should not continue assigning stuff at all, or initialize an empty string on those 3 variables. – Kevin Aug 01 '14 at 03:13

1 Answers1

0

First of all I'm believing that you have an input element of this sort in your html:

<label for='refOff'>Reference Number: </label>
<input type='text' id='refOff' name='refOff'/> 

This line in your code:

$refnum = (isset($_POST['refOff'])) 

only returns a boolean value (i.e. true or false) and never returns the actual value the user has entered into the 'refOff' html input element. This should rather work well using the ternary operator:

$refnum = (isset($_POST['refOff']))? $_POST['refOff'] : null;
if($refnum){
$query = mysql_query("SELECT * FROM pilot WHERE geo=$refnum");
// display query results
while($row = mysql_fetch_array($query))
 {
    $rname =$row['rname'];
    $pname =$row['pname'];
    $mname =$row['mname'];                           
 } 
}

Goodluck!