Please use parameterized queries:
$mysqli = new mysqli('host', 'user', 'pass', 'db', port);
if($result = $mysqli -> prepare("SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid=?")) {
$result -> bind_param("i", $pid1); // Assuming pid1 is an integer
$result -> execute();
$result -> store_result();
$result -> bind_result($pid);
while($result -> fetch()) {
echo $pid; // etc etc
}
}
Reading for you:
- http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
- https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet
Similar to Drew's suggestion:
<?php
$mysqli = new mysqli('host', 'user', 'pass', 'db', port);
$order_string = ""
// Assuming PHP >= 5.4, otherwise use array()
$sort_keywords = [
"namn" => "lastname",
"email" => "email",
];
if(isset($_GET['sort']) && array_key_exists($_GET['sort'], $sort_keywords)) {
$order_string = " ORDER BY ".$sort_keywords[$_GET['sort']];
}
// Assume <head>, <body> etc etc here.
echo "<br><table><tr class='tabletop'><th><a href='mypage.php?sort=namn'>Namn</a></th><th><a href='mypage.php?sort=email'>E-mail</a></th><th>Resultat</th><th>Ta bort kandidat</th></tr>";
if($result = $mysqli -> prepare("SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid=?".$order_string)) {
$result -> bind_param("i", $pid1); // Assuming pid1 is an integer
$result -> execute();
$result -> store_result();
$result -> bind_result($rid, $pid, $firstname, $lastname, $email);
while($result -> fetch()) {
// $color? $result00?
echo "<tr><td><strong>
<form action='respondent2.php' method='GET'>
<input type='hidden' name='rid' value='".$rid."'>
<input type='hidden' name='firstname' value='".$firstname."'>
<input type='submit' class='resname' name='submit' value='".$firstname." ".$lastname."'>
</form>
</strong></td>
<td>".$email."</td>
<td><strong><span style=\"color: $color\">".$result00."</span>%</strong></td>
<form action='deleterespondent2.php' method='post'>
<input type='hidden' name='rid' value='".$rid."'>
<td> <input type='submit' class='mydel' value='Radera' onclick=\"return confirm('Show me!')\">
</form>
</td></tr>";
}
}
echo "</table>";
?>
Or (exactly the same):
<?php
$mysqli = new mysqli('host', 'user', 'pass', 'db', port);
$order_string = ""
// Assuming PHP >= 5.4, otherwise use array()
$sort_keywords = [
"namn" => "lastname",
"email" => "email",
];
if(isset($_GET['sort']) && array_key_exists($_GET['sort'], $sort_keywords)) {
$order_string = " ORDER BY ".$sort_keywords[$_GET['sort']];
}
// Assume <head>, <body> etc etc here.
?>
<br><table><tr class='tabletop'><th><a href='mypage.php?sort=namn'>Namn</a></th><th><a href='mypage.php?sort=email'>E-mail</a></th><th>Resultat</th><th>Ta bort kandidat</th></tr>
<?php
if($result = $mysqli -> prepare("SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid=?".$order_string)) {
$result -> bind_param("i", $pid1); // Assuming pid1 is an integer
$result -> execute();
$result -> store_result();
$result -> bind_result($rid, $pid, $firstname, $lastname, $email);
while($result -> fetch()) {
// $color? $result00?
?>
<tr><td><strong>
<form action='respondent2.php' method='GET'>
<input type='hidden' name='rid' value='<?php echo $rid; ?>'>
<input type='hidden' name='firstname' value='<?php echo $firstname; ?>'>
<input type='submit' class='resname' name='submit' value='<?php echo $firstname; ?> <?php echo $lastname; ?>'>
</form>
</strong></td>
<td><?php echo $email; ?></td>
<td><strong><span style="color: <?php echo $color; ?>"><?php echo $result00; ?></span>%</strong></td>
<form action='deleterespondent2.php' method='post'>
<input type='hidden' name='rid' value='<?php echo $rid; ?>'>
<td> <input type='submit' class='mydel' value='Radera' onclick="return confirm('Show me!')">
</form>
</td></tr>
<?php
}
}
?>
</table>
Also, probably best to define the sort array in the database and have them echo
ed as an attribute in the html table row header. I would personally just sort on the client side, which is actually sometimes quicker (ORDER BY
can be slow), however, I am unaware of your intentions.
Have fun with the edits ;)