Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to search data from a mysql database and display on html text fields, how ever when ever im searching it display error messages inside the data field

Database name : latgsjason
table name : customer

values which i needs are as following


veh_regno
nic
fullname
address
dob
contact
color

there are other details but all i need these details to be search and display

What I have tried:

<?php
    
    
if(isset($_POST['search']))
{
    
    $connect= mysqli_connect("localhost","root","1234","latgsjason");
    if (mysqli_connect_error()){
    echo"Failed to Connect";
    }
//    $result=mysqli_query($connect,"SELECT * FROM customer WHERE veh_regno='$_POST[V_Regno]'");
    $Vregno = $_POST['V_Regno'];
    $query="SELECT 'nic','fullname','address','contact','color' FROM customer WHERE 'veh_regno'=$Vregno LIMIT 1"; 
    $result=mysqli_query($connect,$query);
    
    while ($row=mysqli_fetch_array($result))
        {
            $VReg=$row['veh_regno'];
            $Nic=$row['nic'];
            $FName=$row['fullname'];
            $Address=$row['address'];
            $Contact=$row['contact'];
            $Color=$row['color'];
        }
        
        mysqli_free_result($result);
        mysqli_close($connect);
}
else
{
    $VReg="";
    $Nic="";
    $FName="";
    $Address="";
    $Contact="";
    $Color="";
}

?>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Manage Account</title>
        <link rel="stylesheet" type="text/css" href="CSS/Style SheetGeneral.css">
    </head>
<body>
<br><br><br>

<table background="Images/Back Ground Images/BG1.jpg" border="2" align="center" width ="825" height="820">

    <th align=center valign=center>

        <form action="ManageAccount.php" method="POST"/>

        <div class="textcenter1">
            <h2>Manage An Account</h2>
        </div>
        <br><br>

        <table border="0"; align="center" width ="655" height="550" >
        <tr>
            <td>
                VEHICLE REGISTRATION NUMBER :
            </td>
            <td>
                <input type="text" name="V_Regno" size="40" value="<?php echo $VReg; ?>">
            </td>
        </tr>
        
        <tr>
            <td>
                NATIONAL ID :
            </td>
            <td>
                <input type="text" name="Nic"  size="40" value="<?php echo $Nic; ?>">
            </td>
        </tr>
        
        <tr>
            <td>
                FULL NAME :
            </td>
            <td>
                <input type="text" name="F_Name"  size="40" value="<?php echo $FName; ?>">
            </td>
        </tr>
        
        <tr>
            <td>
                ADDRESS :
            </td>
            <td>
                <input type="text" name="Address" size="40" value="<?php echo $Address; ?>">
            </td>
        </tr>
        
        <tr>
            <td>
                TELEPHONE NUMBER/MOBILE NUMBER :
            </td>
            <td>
                <input type="text" name="Contact" size="40" value="<?php echo $Contact; ?>">
            </td>
        </tr>
        
        <tr>
            <td>
                COLOR :
            </td>
            <td>
                <input type="text" name="Color" size="40" value="<?php echo $Color; ?>">
            </td>
        </tr>
        
<tr>
<td>
NIC Attachment :<select name="NicChk">
                <option selected="" value="">(New Document?)</option>
                <option value="yes">YES</option>
                <option value="no">NO</option>
                </select>
</td>
<td>
    <input type="file" name="Nic_Img" accept="image/*">
</td>
</tr>
<tr>
<td>
Registration Certificate :  <select name="RCcheck">
                            <option selected="" value="">(New Document?)</option>
                            <option value="yes">YES</option>
                            <option value="no">NO</option>
                            </select>
</td>
<td>
    <input type="file" name="Reg_Cer" accept="image/*">
</td>
</tr>

<tr>
<td></td>
<td>
    <input align="left" type="image" src="Images/Buttons/button_search.png" alt="button" name="search" value="Search" width="110" height="50"/>
</form> 
        
 <!--if submit button and reset button in same <form> will perform the formvalidation which leads the reset button also work as a submit button(wont reset anything)-->
 
 <form action="UpdateAccount.php" method="POST"/>
    <input align="center" type="image" src="Images/Buttons/button_update.png" alt="" name="reset" value="reset" width="110" height="50"/>
</td>
</tr>

</form>
 
</table>


</th>
</table>
<br><br>

</body>
</html>
Posted
Updated 16-Jul-17 8:15am
v2

It is about variable scope...
You variable is scoped or in the while or in the else part of your code and unknown for the HTML part...
Define them before the first if to make them scope for the entire code, including HTML...

Beside this... It seems to me that you have a serious logical glitch in your code...
See your code with my eyes...
if(whatever) {
  ...read data from database...
  ...loop over records from the database...
    ...push the current record into the variables...
}
else {
  ...set all variables to empty...
}

...display data...

With this code toy may display nothing or the last row from the database, which seems to me as the wrong option...
 
Share this answer
 
There is nothing wrong in my HTML part but below code works fine. However,when the form load undefined variable error message appears inside the text field, but u still can type and search , the results are working as expected. if someone can help me out to remove those things inside the text field while it loading , please mention it.

<?php
  
if(isset($_POST['search']))
{
    
    $con= mysqli_connect("localhost","root","1234","latgsjason");
    if (mysqli_connect_error()){
    echo"Failed to Connect";
    }
     $VRegno="";
     $Nic="";
     $FullName="";
     $Address="";
     $Contact="";
     $Color="";

    $result=mysqli_query($con,"SELECT * FROM customer where veh_regno='$_POST[V_Regno]' ");
    
    while ($row=mysqli_fetch_array($result))
        {
            $VRegno=$row['veh_regno'];
            $Nic=$row['nic'];
            $FullName=$row['fullname'];
            $Address=$row['address'];
            $Contact=$row['contact'];
            $Color=$row['color'];
        }
               
        mysqli_close($con);
}  
 
Share this answer
 
This will remove undefined variable error
use variable declaration before if condition

<?php
     $VRegno="";
     $Nic="";
     $FullName="";
     $Address="";
     $Contact="";
     $Color="";

     if(isset($_POST['search']))
     {
      // search query and other codes goes as usual
     }


The reason is you are setting variables inside a if condition, at first (when page loads) if(isset($_POST['search'])) returns false so undefined variable error will shown in input boxes.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900