Page 1 of 1

Need help to display data from DB

Posted: Tue Dec 29, 2015 6:51 am
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:

Re: Need help to display data from DB

Posted: Tue Dec 29, 2015 7:21 am
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.

Re: Need help to display data from DB

Posted: Tue Dec 29, 2015 8:19 am
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:

Re: Need help to display data from DB

Posted: Tue Dec 29, 2015 9:01 am
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.

Re: Need help to display data from DB

Posted: Tue Dec 29, 2015 1:03 pm
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