0

Well, my code works when the selected row exists in the database, it works perfect. But the problem is when I am selecting a data that doesn't exist at all. It gives me an error that my variables are undefined.

This is the scripting:

<?php 
    include('Nethost.php');
        $patient = $_POST['patientid'];
        $diagnosisid = $_POST['diagnosis'];
        $pname = $_POST['patientname'];
        $d = $_POST['diagnosis'];
        $query = mysql_query("select objective from soapie WHERE patientid='$patient' AND subjective='$diagnosisid' LIMIT 1");

       while($rows = mysql_fetch_assoc($query))
        {
            if($rows==NULL) 
            {
                        echo "No data";         
            }
            else
            {
                        $objective = $rows['objective'];        
            }                       
    }
    ?>

I am displaying it input type text..

<table style="margin-left:auto; margin-right:auto">
    <tr>
    <td>Objective</td>
    </tr>
    <tr>
    <td><input ID="txtobjective" name="objective" value="<?php echo "" .$objective. ""; ?>" type="text" /><br></td>
    </tr>
    </table>

I think this is a common problem and I can't even solve it. I just got back from coding using PHP. Can someone suggest me a good practice to handle this kind of problems. Thanks in advance.

eirishainjel
  • 570
  • 7
  • 25

3 Answers3

1

Put $objective='' before code. Your variable has not been set.

<?php 
    include('Nethost.php');
        $patient = $_POST['patientid'];
        $diagnosisid = $_POST['diagnosis'];
        $pname = $_POST['patientname'];
        $d = $_POST['diagnosis'];
        **$objective='';**
        $query = mysql_query("select objective from soapie WHERE patientid='$patient' AND subjective='$diagnosisid' LIMIT 1");

       while($rows = mysql_fetch_assoc($query))
        {
            if($rows==NULL) 
            {
                        echo "No data";         
            }
            else
            {
                        $objective = $rows['objective'];        
            }                       
    }
    ?>
Mehman Bashirov
  • 114
  • 1
  • 11
  • 1
    You save my life! Thank you so much Sir :) I can now run this on an android application and move on to the next problem. Thanks! – eirishainjel Mar 27 '14 at 06:38
1

$fname, $midd, $sname, $objective You need to initallize all the variables you use, so that if there is no value, you don't get the undefined error.

Best practice would be initializing your variables before you use them:

  $objective = $fname = $sname = $midd = '';
  while( $rows = mysql_fetch_assoc ( $query ) ) {
    if( empty( $rows ) )  {
                echo "No data";
    }
    else {
        //check if the variable has value, otherwise set it to null
        $objective = isset( $rows['objective'] ) ? $rows['objective'] : '' ;
    }                    
  }

Also, while echoing the variable, you don't need the " quotes:

 <td><input ID="txtobjective" name="objective" value="<?php echo $objective; ?>" type="text" /><br></td>

 <?php echo $objective; ?>

this would do fine for you.

sven
  • 775
  • 5
  • 14
0

try this i hope this will help you

while($rows = mysql_fetch_assoc($query))
    {
        if(empty($rows))  
        {
                    echo "No data";         
        }
        else
        {
                    $objective = $rows['objective'];        
        }                       
}
Dexter
  • 1,804
  • 4
  • 24
  • 53