简体   繁体   中英

Create Autofill form with PHP

So im still relatively new to php and im trying to create a form that autofill's with information from a table. The table is called students and has the students id, first and last name, and their address. Im trying to make it so it only autofill's with the information of the student with a certain id, for example if the students id is 102 it fills the textboxes with their info.

I have some code which i thought would work but its telling me somethings wrong with the while loop and i don't know what.

Edit: The error i keep getting is this:"mysqli_fetch_assoc() expects parameter 1 to be mysqli_result".

PHP code:

<?php
        
require_once ('Connection.php');

$query = "SELECT * from students where Id = 102";

$result = mysqli_query($dbc, $query);

while ($row = mysqli_fetch_assoc($result)) {
 $row['student_id'];
 $row['stu_Fname'];
 $row['stu_Lname'];
 $row['stu_addr'];

}   
        
?>

Html Form:

<form action="http://localhost/test.php" method="post">
        
        <p>Student ID:
            <input name="stu_id" size="5" value="<?php echo $row['student_id']; ?>" pattern="^\d{3}" required autofocus /><?php echo $row['student_id']; ?>
        </p>
        
        <p>First Name:
            <input type="text" name="fname" size="30" value="<?php echo $row['stu_Fname']; ?>"/>
        </p> 
            
        <p>Last Name:
            <input type="text" name="lname" size="30" value="<?php echo $row['stu_Lname']; ?>"/>
        </p>
        
        <p>Address:
            <input type="text" name="address" size="30" value="<?php echo $row['stu_addr']; ?>"/>
        </p>
        
        <p>
            <input type="submit" name="submit" value="Send"/>
        </p>
    </form>

EDIT: Connection code:

<?php
        
        DEFINE ('DB_User', 'testuser');
        DEFINE ('DB_Password', 'abc123*');
        DEFINE ('DB_Host', 'localhost');
        DEFINE ('DB_Name', 'student');
        
        
        //start database connection
        $dbc = new mysqli(DB_Host, DB_User,DB_Password,DB_Name);
            
            if(mysqli_connect_errno())
            {
                printf("can't connect to database.", mysqli_connect_error());
                exit();
            }   
    
    ?>
while ($row = mysqli_fetch_assoc($result)) {
 $row['student_id'];
 $row['stu_Fname'];
 $row['stu_Lname'];
 $row['stu_addr'];
}   

You're not actually accomplishing anything within the while loop above. There is no assignment, nor output.

Each iteration of the while loop will grab a row (if there is one in the database). Assuming that the row has the four columns you've listed, you can print the data to the screen. For example:

while ($row = mysqli_fetch_assoc($result)) {
  echo $row['student_id'] . ", ";
  echo $row['stu_Fname'] . ", ";
  echo $row['stu_Lname'] . ", ";
  echo $row['stu_addr'];
}  

Assuming you're outputting a form for each student, you can place the form logic within the while loop.

The problem is in your query. Try changing

$query = "SELECT * from students where Id = 102";

into this

$query = "SELECT * from students where Id = '102'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM