Jump to content

Need Drop down menu to display information based on username


phunnydoode

Recommended Posts

Hello my problem here is to have a drop down menu which gathers usernames from the database. Then with a click of a button the information for that specific user selected is shown. I'm close code wise but right now it's just showing me all users. I'm displaying the info in text box's to allow an admin to change the info. 

<form action="edit.php" method="post">

	 <select name="username" id="username">
	 	  <?php 
	  
 // Connects to your Database 
  $con=mysqli_connect('localhost', 'root', '');

/* check connection */

if (mysqli_connect_errno($con)) {
    trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
}

$query = "SELECT `username` FROM `bencobricks` . `users`";

$result = mysqli_query($con, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($con), E_USER_ERROR);

if($result) {
	while($row = mysqli_fetch_assoc($result)) {
		//printf ("%s\n %s\n ",
		echo	"<label for='username'>Username: </label> <option value='$row[username]'>$row[username] </option><br/>";
			
		echo "<br/><br/> ";
		
	}
} 

mysqli_close($con);

?>
	 </select>
<br />
  <input name="send" id="send" type="submit" value="Edit User" />
</form>

Then in the edit.php file i have this code: 

<?php 

// Connects to your Database
$con=mysqli_connect('localhost', 'root', '');

/* check connection */

if (mysqli_connect_errno($con)) {
	trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
}

if (isset($_POST['send'])) {
 $username = $_POST['username'];
 
 $query = "SELECT * FROM `bencobricks` . `users` ";
 $result = mysqli_query($con, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($con), E_USER_ERROR); 
// Print the result

 if($result) {
while ($row = mysqli_fetch_assoc($result)) {
//	$query = "SELECT * FROM `bencobricks` . `users` WHERE `username` = username = $row[username]";
 printf ("%s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
			"<label for='username'>Username: </label> <input type='text' name='username' value='$row[username]'>" . "</input><br/>",
			"<label for='email'>Email: </label> <input type='text' name='email' value='$row[email]'>" . "</input><br/>",
			"<label for='membership'>Membership: </label> <input type='text' name='membership' value='$row[membership]'>" . "</input><br/>",
			"<label for='firstName'>First name: </label> <input type='text' name='firstName' value='$row[firstName]'>" . "</input><br/>",
			"<label for='lastName'>Last name: </label> <input type='text' name='lastName' value='$row[lastName]'>" . "</input><br/>",
			"<label for='gender'>Gender: </label> <input type='text' name='gender' value='$row[gender]'>" . "</input><br/>",
			"<label for='dateOfBirth'>Birthdate: </label> <input type='text' name='dateOfBirth' value='$row[dateOfBirth]'>" . "</input><br/>",
			"<label for='date'>Date: </label> <input type='text' name='date' value='$row[date]'>" . "</input><br/>",
			"<label for='sets'>Sets: </label> <input type='text' name='sets' value='$row[sets]'>" . "</input><br/>",
			"<label for='checkbox'>Checkbox: </label> <input type='text' name='checkbox' value='$row[checkbox]'>" . "</input><br/>",
			"<label for='admin'>Admin? </label> <input type='text' name='admin' value='$row[adminFlag]'>" . "</input><BR>",
			
		"<br/><br/> ");
}
}
} 
mysqli_close($con);
?>

Any help is greatly appreciated. 

In your edit code you need to apply a WHERE clause to the query so it only returns the row where username matches. Eg

 $query = "SELECT * FROM `bencobricks` . `users` WHERE username = '$username'";

However you should not use user input in a query like this. Instead I recommend you to use prepared statements.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.