1

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!';
}
?> 
11
  • 1
    where is the form? Is it in another PHP page? And when exactly do you want it to auto-fill the values? Do you mean restoring the values to the page after an incorrect login, so the user can try again? In that case I would maybe autofill the username but never the password Commented Jul 12, 2017 at 13:49
  • 2
    Always start by Reading the Manual And some tutorials Thats why people spent hours of their time writing it Commented Jul 12, 2017 at 13:53
  • 2
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Jul 12, 2017 at 14:03
  • 1
    Try storing your variables in $_SESSION[' '] like in @WhiteHox answer. Commented Jul 12, 2017 at 14:12
  • 1
    Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack. It is not necessary to escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding. Commented Jul 12, 2017 at 14:18

1 Answer 1

1

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>';
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Syntax checker required
Your quotes surrounding the values should be swapped. You currently have syntax-errors.
Still not properly, no - there's still a syntax error. You're still not concating them. Have a look at the manual: php.net/manual/en/language.operators.string.php - You should escape it like value=\"$name\"<br>, or use single-quotes like you do everywhere else, value='$name'<br>
Forgot my quotes. Thanks
@WhiteHox Thanks a lot. The inputs worked in html. The data is nicely shown in the php file. However, the data won't go over to the html file from php. I've tried to put the session variable in the input itself <INPUT TYPE=TEXT NAME="Username" SIZE=64 TABINDEX=1 class="input" value="'.$_SESSION['Username'];'"> but it seems to just print out the value itself but not the username. I <?php session_start(); ?> at the top of the html file and entered data as shown in "Your form file".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.