Brief synopsis: When I select a user from a dropdown (which populates from Users table), I want the resulting page to show the specific details for that user in a form based view with the current values already filled in based on current data in my database. Database Values are not appearing, instead showing $current_userlastname
on the page. Code below:
if ($dbConnected) {
$UserID = $_POST['UserID'];
$sql = "SELECT * FROM Users WHERE ID = '$UserID'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$current_userlastname = $row['User_LastName'];
$current_userfirstname = $row['User_FirstName'];
$current_useremail = $row['User_Email'];
$current_username = $row['UserName'];
$current_userpassword = $row['UserPassword'];
}
echo '<h2 style = "font-family: arial,helvetica,sans-serif;"> User EDIT </h2>';
echo '<form name="postUser" action="UserUpdate.php" method="POST">';
echo '<input type="hidden" name="UserID" value=".$UserID."/>';
echo '
<table>
<tr>
<td>Last Name</td>
<td><input type="text" name="User_LastName" value="$current_userlastname" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="User_FirstName" value="$current_userfirstname" /></td>
</tr>
<tr>
<td>Email Address</td>
<td><input type="text" name="User_Email" value="$current_useremail" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="UserName" value="$current_username" /></td>
</tr>
<tr>
<td>User Password</td>
<td><input type="text" name="UserPassword" value="$current_userpassword" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="save" /></td>
</tr>
</table>
';
echo '</form>';
You should always use mysqli_*
instead of mysql_*
.
<?php
$conn = mysqli_connect("hosta", "username", "password", "database");
$UserID = $_POST['UserID'];
$sql = "SELECT * FROM Users WHERE ID='$UserID'";
$query = mysqli_query($conn, $sql);
if ($row = mysqli_fetch_assoc($query)) {
$current_userlastname = $row['User_LastName'];
$current_userfirstname = $row['User_FirstName'];
$current_useremail = $row['User_Email'];
$current_username = $row['UserName'];
$current_userpassword = $row['UserPassword'];
echo '<h2 style = "font-family: arial,helvetica,sans-serif;"> User EDIT </h2>';
echo '<form name="postUser" action="UserUpdate.php" method="post">';
echo '<input type="hidden" name="UserID" value="'.$UserID.'"/>';
echo '<table>
<tr>
<td>Last Name</td>
<td><input type="text" name="User_LastName" value="'.$current_userlastname.'" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="User_FirstName" value="'.$current_userfirstname.'" /></td>
</tr>
<tr>
<td>Email Address</td>
<td><input type="text" name="User_Email" value="'.$current_useremail.'" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="UserName" value="'.$current_username.'" /></td>
</tr>
<tr>
<td>User Password</td>
<td><input type="text" name="UserPassword" value="'.$current_userpassword.'" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="save" /></td>
</tr>
</table>';
echo '</form>';
}
In single quoted strings, variables are not parsed.
So if you have a variable, $foo = "bar"
, then
echo '$foo';
Will print $foo
.
But
echo "$foo";
Will print bar
.
Do not use 'echo' for large amounts of HTML. PHP is a 'templating language. You 'switch' into it (<?php) when you want to do processing and out of it (?>) when you just want to send the html to the client. note: <?= is short for: <?php echo.
I cannot provide tested code as the 'mysql_*' stuff is depreciated. Whatever:
syntax checked:
<!DOCTYPE HTML">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<title>Q22942773</title>
</head>
<body>
<?php
/* debug - delete */ var_dump('_POST', $_POST, __FILE__, __LINE__, '<br />');
/* debug - delete */ var_dump('dbConnected', $dbConnected, __FILE__, __LINE__, '<br />');
if ($dbConnected) {
$UserID = $_POST['UserID'];
$sql = "SELECT * FROM Users WHERE ID = '$UserID'";
/* debug - delete */ var_dump('SELECT * FROM Users... ' $sql, __FILE__, __LINE__, '<br />');
$result = mysql_query($sql);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
/* debug - delete */ var_dump('row => ', $row, __FILE__, __LINE__, '<br />');
if ($row) { // only expect one row so let the code show that rather than imply
// more than one row with a while loop.
$current_userlastname = $row['User_LastName'];
$current_userfirstname = $row['User_FirstName'];
$current_useremail = $row['User_Email'];
$current_username = $row['UserName'];
$current_userpassword = $row['UserPassword'];
}
}
// drop out of PHP mode -- all the stuff after is sent to the client
// switch back into PHP mode to do processing of the $current_* variables
// note: <?= is short for: '<?php echo'
?>
<h2 style = "font-family: arial,helvetica,sans-serif;"> User EDIT </h2>
<form name="postUser" action="UserUpdate.php" method="POST">
<input type="hidden" name="UserID" value=".$UserID."/>
<table>
<tr>
<td>Last Name</td>
<td><input type="text" name="User_LastName" value="<?= $current_userlastname ?>" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="User_FirstName" value="<?= $current_userfirstname ?>" /></td>
</tr>
<tr>
<td>Email Address</td>
<td><input type="text" name="User_Email" value="<?= $current_useremail ?>" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="UserName" value="<?= $current_username?>" /></td>
</tr>
<tr>
<td>User Password</td>
<td><input type="text" name="UserPassword" value="<?= $current_userpassword ?>" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="save" /></td>
</tr>
</table>
</form>
</body>
</html>