Reputation: 25
I tried to use other ways but the result only displays one Link edit, since I have 3 kinds of Data/3 Persons, the edit links only shows to one person. I want to put 3 edit links with the same last name and will be updated later on the database.
$mysqli = new mysqli("localhost","root","","logindb");
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM precferm WHERE lname LIKE '$search%'");
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$lname = $rows ['lname'];
$fname = $rows ['fname'];
$mname = $rows ['mname'];
$output = " <a href=edit.php?id=$output> Edit</a><br /> Last Name: $lname<br />First Name: $fname<br />Middle Name: $mname<br /><br />";
}
}else{
$output = "No results";
}
}
Upvotes: 0
Views: 51
Reputation: 22760
As stated by CBroe in comments:
You are overwriting
$output
in each loop iteration, so after the loop only the last value “survives”. You need to append to the variable (that you initialized with an empty string before the loop), if you want to get such links for all three records.
So:
You can use a PHP String operator to Append strings, making your string contain more data with each iteration of the loop.
I have also used "string".$var." more string"
to illustrate the other type of string operator (concatenation) (as well as best practise for variables withi a string setting).
$output = ""; // ensure value starts empty.
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$lname = $rows ['lname'];
$fname = $rows ['fname'];
$mname = $rows ['mname'];
// note the .= appends data to the original $output
$output .= " <a href=edit.php?id=".$output."> Edit</a><br /> Last Name: ".$lname."<br />First Name: ".$fname."<br />Middle Name: ".$mname."<br /><br />";
}
}else{
// note the .= appends data to the original $output
$output .= "No results";
}
}
print $output // This will now output all itterations of the MySQL data loop.
<a href=edit.php?id=".$output.">
This is adding the whole string ($output
) to the id GET clause, and is incorrect. What you want here would be some sort of id
counter; such as
<a href=edit.php?id=".$rows['id'].">
Upvotes: 0
Reputation: 141
the problem was that you were linking to a page with incorrect id, you should specify the link with a number to edit the row, I just added $id to route to correct page. good luck
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$lname = $rows ['lname'];
$fname = $rows ['fname'];
$mname = $rows ['mname'];
$id = $row['id'];
$output = " <a href='edit.php?id=$id'> Edit</a><br /> Last Name: $lname<br />First Name: $fname<br />Middle Name: $mname<br /><br />";
}
}else{
$output = "No results";
}
Upvotes: 2
Reputation: 175
It looks like you need to provide an id
value for the URL:
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$id = $rows['id'];//something like this
$lname = $rows['lname'];
$fname = $rows['fname'];
$mname = $rows['mname'];
$output = " <a href=edit.php?id=$id> Edit</a><br /> Last Name: $lname<br />First Name: $fname<br />Middle Name: $mname<br /><br />";
}
}else{
$output = "No results";
}
}
Upvotes: 2