I've had a look around for an answer but unfortunately nothing has worked in my case.
EDIT: Different than other answers as code is required to be intentionally vulnerable, ignoring best practice.
I'm developing a feature on a web page which basically allows the user to, using a form, look up and see if a username is in a table in the database. If it is, the result of the query should be echo'd or displayed back to them. This might sound odd, but the web page in question is to teach web vulnerabilities and the point of this exercise is for students to use SQL Injection to obtain the password for that username as well.
Here is the PHP...
<?php
$error=''; //
if(isset($_POST['submit'])){
if(empty($_POST['user'])){
$error = "Please enter a username.";
}
else
{
//Define $user
$user=$_POST['user'];
//Establish Connection with server by passing server_name, user_id
and pass as a parameter
$sqli = mysqli_connect("localhost", "root", "");
//Select Database
$db = mysqli_select_db($sqli, "#");
$query = mysqli_query($sqli, "SELECT username FROM sqlusers WHERE username
='$user'");
$results = mysqli_query($query);
$rows = mysqli_num_rows($query);
if($rows == 1){
echo $query;
}
else
{
$error = "Username is not in the database";
}
mysqli_close($sqli); //Close Conenction
}
}
?>
In the HTML I also have..
<?php echo $error; ?>
The feature should work as follows. Student uses form to check for Username1 in the database. Username1 is in the database, so they receive an echo saying 'Username1' is in the database. Then, using SQL Injection, they alter the query to select the password for that username as well, and are shown 'Username 1 Password1' is in the database.
I'm still learning PHP so please excuse obvious mistakes.
Thank you for your help and any assistance or tips would be greatly appreciated!