Connecting MySQL with PHP
(SELECT statement)
Dr. Ali Allam Web Programming (BIS317E)
Example (1): Retrieving/selecting records from a
table
Retrieving records from the Employee table
Suppose that the
EMPLOYEE table
contains the shown
records.
Retrieving records from the Employee table
We want to retrieve the EmpID, Name, Salary of employees whose
salaries are above an amount entered by the user.
<html>
<body>
<form action="report.php">
<h3> Enter the salary amount </h3>
<input type="text" name="num">
<input type="submit">
</form>
</body>
</html>
Retrieving records from the Employee table
<html><body>
<table border="1"><tr><th>ID</th><th>Name</th><th>Salary</th></tr>
<?php
$num = $_GET["num"];
$conn = mysqli_connect("localhost", "root", "", "ali db");
$stmt = "select * from `employee` where `salary` >= '$num' ";
$result = mysqli_query($conn, $stmt);
while ($row = mysqli_fetch_array($result) )
{
echo"<tr><td>$row[0]</td><td>$row[1]</td><td>$row[4]</td></tr>";
}
?>
</table>
</body></html>
Retrieving records from the Employee table
Example (2): Creating a login page
Creating a login page
Suppose that the
employee table
contains the shown
records.
Creating a login page
We want to design a login page, where the user enters his username and
password. IF the username/password combination is found in the table,
the program displays the employee’s profile. If the user enters an invalid
username/password, the program prints an error message.
<html>
<body>
<form action="report.php" method="post">
Username <input type="text" name="username"><br>
Password <input type="password" name="password"><br>
<input type="submit" value="Sign in">
</form>
</body>
</html>
Creating a login page
<?php
$user = $_POST["username"];
$pwd = $_POST["password"];
$conn = mysqli_connect("localhost","root","","ali db");
$stmt = "select * from `employee` where `username`='$user' AND `password`='$pwd'";
$result = mysqli_query($conn, $stmt);
if ($row = mysqli_fetch_array($result) )
echo "<h3>Welcome $row[1]</h3>Username: $row[2]<br>Salary: $row[4] <br>Gender: $row[5]";
else
echo"Login failed. Invalid username or password."; // or header("Location: invalidform.html");
?>
Creating a login page
Example (3): Creating a dynamic input form
<html>
<body>
<form action="add_emp.php" method="post">
Employee ID <input type="text" name="empid"><br>
Full Name <input type="text" name="fullname"><br>
For the input form of an Username <input type="text" name="user"><br>
Password <input type="password" name="pwd1"><br>
employee … Confirm Password <input type="password" name="pwd2"><br>
Problem: suppose that Gender <input type="radio" name="gen" value="Male" checked>Male
the shown list of <input type="radio" name="gen" value="Female">Female<br>
departments was Salary <input type="text" name="salary"><br>
updated/changed. Department <select name="department">
<option value="101">Finance</option>
Solution: it’s a good idea <option value="201">Marketing</option>
to display a dynamic list <option value="301">Human Resources</option>
of the departments found <option value="401">Technical Support</option></select><br>
in the database, rather <input type="submit" value="Add Employee"><input type="reset">
than displaying a static </form>
</body>
list of the departments.
</html>
Problem: the company added two new
departments, but the drop-down list was not
updated.
<html><body>
<form action="add_emp.php" method="post">
Employee ID <input type="text" name="empid"><br>
Full Name <input type="text" name="fullname"><br>
Username <input type="text" name="user"><br>
Password <input type="password" name="pwd1"><br>
Solution: retrieve Confirm Password <input type="password" name="pwd2"><br>
and display the list Gender <input type="radio" name="gen" value="Male" checked>Male
of departments <input type="radio" name="gen" value="Female">Female<br>
Salary <input type="text" name="salary"><br>
stored in the Department <select name="department">
DEPARTMENT table. <?php
$conn= mysqli_connect("localhost", "root", "", "ali db");
$stmt = "SELECT `dept num`, `name` FROM `department`";
$result =mysqli_query($conn,$stmt);
while( $row = mysqli_fetch_array($result) )
echo"<option value=$row[0]> $row[1] </option>";
?> </select><br>
<input type="submit" value="Add Employee"><input type="reset">
</form></body></html>
The problem is solved forever !
Any update in the departments will be
reflected in the drop-down list ☺