Reputation: 428
I have made a search function which outputs peoples names and their details after searching the first name, but when there are multiple people with the same first name in the database the output only shows one result. Here is my code:
<?php
include ('connect-db.php');
if (isset($_GET ['forename'])){
$forename = $_GET['forename'];
$userquery = mysql_query("SELECT * FROM staff WHERE forename = '$forename'") or die ("error getting information from database.");
if (mysql_num_rows($userquery) == null ){
die ("No staff directory found");
}
while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC)){
$firstname = $row['forename'];
$lastname = $row['surname'];
$email = $row ['email'];
}
}
?>
<h2><?php echo strtoupper( $forename )?> <?php echo strtoupper ( $lastname )?>'s Profile</h2><br><br><br>
<table style = "font-weight:bold;
font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<hr style="border-top: dotted 1px;" />
<tr><td>First Name:    </td><td><?php echo strtoupper ($firstname) ?> </td></tr>
<tr><td>Last Name: </td><td><?php echo strtoupper ($lastname) ?> </td></tr>
<tr><td>Email Address: </td><td><?php echo strtoupper ($email) ?> </td></tr>
<tr><td>Mobile Number: </td><td> #</td></tr>
<tr><td>Work Number: </td><td> ##</td></tr>
<tr><td>Home Phone: </td><td> ###</td></tr>
</table>
Can anyone tell me what I need to do here as I can't figure out what to add and where. Many thanks in advance, Danny
Upvotes: 1
Views: 72
Reputation: 10548
<?php
include ('connect-db.php');
if (isset($_GET ['forename']))
{
$forename = $_GET['forename'];
$userquery = mysql_query("SELECT * FROM staff WHERE forename = '$forename'") or die ("error getting information from database.");
if (mysql_num_rows($userquery) == null ){
die ("No staff directory found");
}
?>
<h2><?php echo strtoupper( $forename )?> <?php echo strtoupper ( $lastname )?>'s Profile</h2><br><br><br>
<table style = "font-weight:bold;font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<hr style="border-top: dotted 1px;" />
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>MobileNumber</th>
<th>Work Phone</th>
<th>Home Phone</th>
</tr>
<?
while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC))
{
$firstname = $row['forename'];
$lastname = $row['surname'];
$email = $row ['email'];
?>
<tr>
<td><?php echo strtoupper($firstname); ?></td>
<td><?php echo strtoupper($lastname); ?></td>
<td><?php echo strtoupper($email); ?></td>
<td>#</td>
<td>##</td>
<td>###</td>
</tr>
<?php}?>
</table>
<?}?>
Upvotes: 0
Reputation: 5157
You should put your HTML inside loop
<?php
include ('connect-db.php');
if (isset($_GET ['forename'])){
$forename = $_GET['forename'];
$userquery = mysql_query("SELECT * FROM staff WHERE forename = '$forename'") or die ("error getting information from database.");
if (mysql_num_rows($userquery) == null ){
die ("No staff directory found");
}
while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC)){
$firstname = $row['forename'];
$lastname = $row['surname'];
$email = $row ['email'];
?>
<h2><?php echo strtoupper( $forename )?> <?php echo strtoupper ( $lastname )?>'s Profile</h2><br><br><br>
<table style = "font-weight:bold;
font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<hr style="border-top: dotted 1px;" />
<tr><td>First Name:    </td><td><?php echo strtoupper ($firstname) ?> </td></tr>
<tr><td>Last Name: </td><td><?php echo strtoupper ($lastname) ?> </td></tr>
<tr><td>Email Address: </td><td><?php echo strtoupper ($email) ?> </td></tr>
<tr><td>Mobile Number: </td><td> #</td></tr>
<tr><td>Work Number: </td><td> ##</td></tr>
<tr><td>Home Phone: </td><td> ###</td></tr>
</table>
<?php }
}
?>
Upvotes: 1
Reputation: 11830
Put below peice of code in while loop :
<?php
while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC)){
$firstname = $row['forename'];
$lastname = $row['surname'];
$email = $row ['email'];
?>
<h2><?php echo strtoupper( $forename )?> <?php echo strtoupper ( $lastname )?>'s Profile</h2><br><br><br>
<table style = "font-weight:bold;
font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<hr style="border-top: dotted 1px;" />
<tr><td>First Name:    </td><td><?php echo strtoupper ($firstname) ?> </td></tr>
<tr><td>Last Name: </td><td><?php echo strtoupper ($lastname) ?> </td></tr>
<tr><td>Email Address: </td><td><?php echo strtoupper ($email) ?> </td></tr>
<tr><td>Mobile Number: </td><td> #</td></tr>
<tr><td>Work Number: </td><td> ##</td></tr>
<tr><td>Home Phone: </td><td> ###</td></tr>
</table>
<?php}?>
Upvotes: 1