Need help to display data from DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
john7911
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2015 6:21 am

Need help to display data from DB

Post by john7911 »

Hi :D
I don't know how to display values from DB when i select a value from a selection menu, as you can see from the picture, if for exemple i select HEA140 from the menu I want to display the name and radius...
Image
Image

Here is the PHP code:

Code: Select all

<?php
$message = '';
$db = new MySQLi('localhost', 'root', '', 'profile');
if ($db -> connect_error){
	$message = $db -> connect_error;
}else{
	$sql = 'SELECT * FROM hea';
	$result = $db -> query($sql);
	if($db -> error){
		$message = $db -> error;
	}
}
?>

<!DOCTYPE html>
<html>
<header>
<title>Page2</title>
</header>
<body>

<form  method="post">
<?php
if($message){
	echo "$message";
}
echo "<select name='sub1'>";
while ($row = $result -> fetch_assoc()) {
	
	echo "<option value='" . $row['nom'] . "'>" . $row['nom'] . "</option>";
	}
echo "</select>";

if (isset($_POST['afficher'])){
	
	echo "<br>";
	echo 'Nom: ' . $row['nom'];
	echo "<br>";
	
	echo 'Hauteur: ' . $row['hauteur'];
	echo "<br>";
	
	echo 'Base: ' . $row['base'];
	echo "<br>";

	echo 'TW: ' . $row['tw'];
	echo "<br>";
	
	echo 'TF: ' . $row['tf'];
	echo "<br>";	
	
	echo 'Rayon: ' . $row['rayon'];
	echo "<br>";		
	
}
?>
<input type="submit" name="afficher" value="Afficher">
</form>
</body>
</html>

The picture of the DB:
Image

Thank you for your help :wink:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help to display data from DB

Post by Celauran »

Your select is named sub1, so if you want to display details of the selection on form submission, you'd need to use the value of $_POST['sub1'] in a WHERE clause. As you've used SELECT * in your original query, you technically have all the information, so you could also write that to some hidden divs which you'd display when the value of the select changes via a JavaScript event listener.
john7911
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2015 6:21 am

Re: Need help to display data from DB

Post by john7911 »

Thank you Celauran :)
I changed to $_POST['sub1'] but nothing changed.
How can I use WHERE in my case to select all.
I am new in programming :roll: I am reading a book to learn PHP.
:wink:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help to display data from DB

Post by Celauran »

At the beginning of your script, you have a query to select everything from the database. Modifying that query if $_POST['sub1'] is set will give you the desired results. You don't want to lose the initial query or you'd lose your select list, so you'll have to do both. You could do something like this

Code: Select all

<?php
$message = '';
$db = new mysqli('localhost', 'root', '', 'profile');
if ($db->connect_error) {
    $message = $db->connect_error;
} else {
    $sql = 'SELECT * FROM hea';
    $result = $db->query($sql);
    if ($db->error) {
        $message = $db->error;
    }
}

if (isset($_POST['sub1'])) {
    $query = "SELECT * FROM hea WHERE nom = ?";
    $stmt = $db->prepare($query);
    $stmt->bind_param('s', $_POST['sub1']);
    $exec = $stmt->execute();
    if ($exec) {
        $result = $stmt->get_result();
        $data = $result->fetch_assoc();
    }
}
Note the use of prepared statements when passing in user input.
john7911
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2015 6:21 am

Re: Need help to display data from DB

Post by john7911 »

Thank you very much Celauran
http://faouweb.net/profile/hea.php
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
Post Reply