Sassux
Sassux

Reputation: 11

How to auto fill HTML form using PHP and MySQL?

I'm a beginner in coding and wanted to do a form in html that would be connected to MySQL. After that, I wanted to be able to fill the form automatically. What I mean is that for instance if I enter the code, I want the username and the password connected to the code in MySQL fill the form automatically. My html code and php code are in separate files. Right now I use:

<FORM name="form" method="post" action="code.php">

To connect php file with the html file. I use submit button to send the data to MySQL. I have considered a button to fill the data but haven't tested it yet. I also figured out how to fetch data from MySQL:

<?php

//init
$server = 'localhost';
$user = 'root';
$db = 'database';

$con = mysqli_connect($server, $user);

if(!$con) {
    die('Could not connect: ' . mysqli_error());
}

mysqli_select_db($con, $db);

$query = "SELECT * FROM TEST";
$retval = mysqli_query($con, $query);

if(!retval) {
    echo ' Error: Data input failed!' . mysqli_error($con);
}

while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
    echo "User : {$row['Username']}  <br> ".
         "Code : {$row['Code']} <br> ".
         "Password : {$row['Password']} <br> ";
}

mysqli_close($con);
?>

However, I have no idea how to connect the fetched data to html form. If you have any ideas, feel free to share them with me.

--- CODE.PHP ---
<?php

//init
$server= 'localhost';
$user = 'root';
$db = 'database';

//input 
$username = $_POST['username'];
$code = $_POST['code'];                               
$password = $_POST['password'];

$con = mysqli_connect($server, $user);

if(!$con) {
    echo 'No connection!';
}

if(!mysqli_select_db($con, $db)) {
    echo ' No database!';
}
$query = "INSERT INTO TEST (Username, Code, Password) VALUES ('$username', '$code', '$password')";

if(!mysqli_query($con, $query)) {
    echo ' Error: Data input failed! ' . mysqli_error($con);
} else {
    echo ' Data input successful!';
}
?> 

Upvotes: 1

Views: 11300

Answers (1)

codebarz
codebarz

Reputation: 327

You can echo the form and its variable in the while loop used to fetch from database

EDIT

<?php
....

while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {

//echo "User : {$row['Username']}  <br> ".
     //"Code : {$row['Code']} <br> ".
     //"Password : {$row['Password']} <br> ";

//Probably better this way

$name = $row['Username'];
$code = $row['code'];
$password = $row['password'];

echo '<form action="" method="post">
      <input type="text" name="yourname" value="'.$name.'"<br>
     <input type="text" name="yourname" value="'.$code.'"<br> 
     <input type="text" name="yourname" value="'.$password.'"<br> 
</form>';
}

....
?>

if your are trying to access each variable on another file you store them in a $_SESSION and session_start() in the form file. This is depend on how you site workflow is.

<?php
....

while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {

//echo "User : {$row['Username']}  <br> ".
     //"Code : {$row['Code']} <br> ".
     //"Password : {$row['Password']} <br> ";

//Probably better this way

$name = $row['Username'];
$code = $row['code'];
$password = $row['password'];

$_SESSION['password'] = $password;
$_SESSION['code'] = $code;
$_SESSION['Username'] = $Username;

}

....
?>

Your form file

<?php 
//Start session at the top of page
session_start();

$password = $_SESSION['password'];
$code = $_SESSION['code'];
$Username =  $_SESSION['Username'];

 echo '<form action="" method="post">
      <input type="text" name="yourname" value="'.$Username.'"<br>
     <input type="text" name="yourname" value="'.$code.'"<br> 
     <input type="text" name="yourname" value="'.$password.'"<br> 
</form>';
?>

Upvotes: 1

Related Questions