Help, I have a big probem. I wanto to read data from an mysql table and then write this data into a form so that the user can update this data. The code could seem somethin like this, but it doesn't work

<?php

include 'complete.php';

    $user_name = "exa";
    $password = "exa";
    $database = "exa";
    $server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM tb_exa WERE username='$CCGuser' AND password='$CCGpassw'";
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_assoc($result)) {
$nome=$db_field['name'];
$cognome=$db_field['gender'];
}

mysql_close($db_handle);

}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}


 
	

	<table style="width: 100%">
	<form action="process_u.php" method="post">
		<tr>
			<td>Name: </td>
			<td> <input type="text" name="name" value="$nsme"></td>
		</tr>
		<tr>
			<td>Gender: </td>
			<td> <input type="text" name = "gender" value="$gender"></td>
		</tr>

 <input type="submit" value="GO" name="Submit">&nbsp; <input type="reset" name="reset" value="Reset" /></td>
			<td>&nbsp;</td>
		</form></tr>
	</table>

My problem is: How to read form the sql table and write the result on the form.

*the variables $CCGuser and $CCGpssw are written in complete.php (see top of the code) that's included in here

Dani AI

Generated

Notes and a safer pattern for (building on ’s corrections)

The original problems were mostly simple: a SQL keyword typo, mismatched variable names, and HTML left inside an open PHP block. Beyond those fixes, two important improvements are recommended: use parameterized queries (PDO or mysqli) to avoid SQL injection, and escape output with htmlspecialchars() when inserting values into form inputs. The example below shows a minimal, modern workflow for selecting a single row, populating a form, and then updating that row.

<?php
// complete.php provides $CCGuser and $CCGpassw
$pdo = new PDO('mysql:host=localhost;dbname=exa;charset=utf8mb4','exa','exa',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('SELECT id, name, gender FROM tb_exa WHERE username = ? AND password = ? LIMIT 1');
$stmt->execute([$CCGuser, $CCGpassw]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<form action="process_u.php" method="post">
  <input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
  <input type="text" name="name" value="<?php echo htmlspecialchars($row['name'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
  <input type="text" name="gender" value="<?php echo htmlspecialchars($row['gender'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
  <input type="submit" value="Update">
</form>

Process the update with a prepared statement and basic validation:

<?php
// process_u.php (validate inputs first)
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
$name = trim($_POST['name'] ?? '');
$gender = trim($_POST['gender'] ?? '');

if ($id && $name !== '') {
  $stmt = $pdo->prepare('UPDATE tb_exa SET name = ?, gender = ? WHERE id = ?');
  $stmt->execute([$name, $gender, $id]);
}

Troubleshooting and best practices: enable exceptions during development (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION), confirm complete.php actually sets the expected variables or use session data, never select by plaintext passwords (use password_hash() / password_verify()), add CSRF protection to forms, and avoid closing PHP tags at the end of pure-PHP files to prevent accidental output. For debugging, inspect $row with var_dump() or log PDO exceptions to the error log rather than exposing them in production.

Recommended Answers

All 2 Replies

There are a couple of mistakes in your code:
1. you declare $nome=$db_field; on line 19 and then use value="$nsme" on line 39 (typo?)
2. you declare $cognome=$db_field; on line 20 and then use value="$gender" on line 43 (shouldn't it be value="$cognome")
3. your <'php opening tag is not followed by a ?> closing tag somewhere before html code
4. your html code is within PHP block (see 3. above) which causes errors
5. in html you must echo variables enclosed in php tags

See the example of how this code should look like:

<?php

include 'complete.php';

    $user_name = "exa";
    $password = "exa";
    $database = "exa";
    $server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM tb_exa WERE username='$CCGuser' AND password='$CCGpassw'";
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_assoc($result)) {
$nome=$db_field['name'];
$cognome=$db_field['gender'];
}

mysql_close($db_handle);

}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>
 
    

    <table style="width: 100%">
    <form action="process_u.php" method="post">
        <tr>
            <td>Name: </td>
            <td> <input type="text" name="name" value="<?php echo $nome;?>"></td>
        </tr>
        <tr>
            <td>Gender: </td>
            <td> <input type="text" name = "gender" value="<?php echo $cognome;?>"></td>
        </tr>

 <input type="submit" value="GO" name="Submit">&nbsp; <input type="reset" name="reset" value="Reset" /></td>
            <td>&nbsp;</td>
        </form></tr>
    </table>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.