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!';
}
?>