2. OBJECTIVES
By the end of this lecture student will be able to:
Apply php methodology for different SQL queries using
MySQLi extension(procedural) through interface (form)
2
4. Insert , Delete , Update
1. create a connection to the database
2. verify the connection
3. create an SQL query
4. preform the Query in the database
5. verify the query (optional)
6. close the database connection (Good
practice to do so(why?))
4
PHP METHODOLOGY:
5. LETS WARM UP
1. Create a new database called lecture7
2. Create a table called students that has 4 column (ID , Fname, Lname ,
Email)
5
6. LETS WARM UP
3. Open a new php page call it lecture7.php
4. Create a form with 3 text fields and one insert button ..
3. Lets write the connection code to the database and verify it?
 Hint: in order to connect and verify our connection 3 functions are needed
 mysqli_connect(with 4 arguments);
 mysqli_connect_errno();
 mysqli_connect_error();
6
Form code is in the note ..
7. CONNECT TO THE DATABASE AND
VERIFY THE CONNECTION
7
Connection code is in the note
..
7
8. NOW,
 In the last lecture (lecture 7) we learned how to write insert code.
1. (step 3: create an insert SQL query )
2. (step 4: preform the insert Query in the database by using mysqli_query()
function )
 In (lecture 5 & 6) we learn how to deal with user input ..
Using your knowledge and based on your understanding. write a code to insert the
user inputs to the database..
8
10. Select
1. create a connection to the database
2. verify the connection
3. create an SQL select query
4. preform the Query in the database
A. receive the result from the database
(As an array)
B. parse the result and use them (use
the index)
5. verify the query (optional)
6. close the database connection
10
PHP
METHODOLOGY:
11. NOW WE NEED TO DISPLAY ALL
RECORDS IN A HTML TABLE
 First lets make sure that select code is
working just fine …
 Lets make step (3 , 4 , a and b )
11
 Select Methodology
1. create a connection to the database
2. verify the connection
3. create an SQL select query
4. preform the Query in the database
A. receive the result from the database
(As an array)
B. parse the result and use them (use
the index)
5. verify the query (optional)
6. close the database connection
12. 12
insert code is in the
note ..
Now lets place those in a table…
Testing select code functionality..
15. THE BROWSER WILL LOOK LIKE
…
Lets say , we would like to update the email of the
second record .. What should we do ?!
All the records should be pleased in a text field in
order to update them 15
16. pleasing the form values in an input
text field
16
Notice that when we want to display the information in text box and we need the end
user to not change the value of this field we can add readonly attribute
17. pleasing the form values in an input text
field (output)
17
Note: the border for the table disappear because we set the
table border to zero
22. LETS TEST THE FORM
22
Try to insert a
record
Delete the last
record
23. LETS TEST THE FORM
23
Is everything
working well?
What happened? Why? How we fix it?
NO
The record will be
deleted and on the
same time
another one with
the same value
will be added
The insert value
was kept on the
supper global
array post
The page should
be reloaded
24.  The header() function sends a raw
HTTP header to a client.
 Return Values: This function
doesn’t return any value.
 Uses:
 Redirect the browser
THE SOLUTION IS:
PHP HEADER() FUNCTION
<?php
// PHP program to describes header function
// Redirect the browser
header(“ Location: http://www.iau.edu.sa
");
// The below code does not get executed
// while redirecting
?>
24
Where to
add?
26. CHALLENGE YOURSELF …
26
 Add an empty fields to allow the end user to insert record in the same
table…
 NOTE: ID field should be read only .. (why?)
//perform only when the user click on insert
if(isset($_POST['insert'])){
// store user inputs in variables
$Fname=$_POST['Fname'];
$lname=$_POST['Lname'];
$Email=$_POST['Email'];
// step 3# create an insert sql query
$sql = "INSERT INTO students (Fname, Lname, Email)
VALUES ('$Fname', '$lname' ,'$Email')";
// step 4# preform the insert Query in the database by using mysqli_query()
$result=mysqli_query($con, $sql);
// verify the query (optional)
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
// step 3# create a Select sql query
$sql = "SELECT ID,Fname,Lname,Email FROM students";
// step 4# preform the insert Query in the database using mysqli_query()
$result= mysqli_query($con, $sql);
// step a & b
while ($rows = mysqli_fetch_array($result)){
echo $rows['ID']. " " .$rows['Fname']. " " . $rows['Lname']. " " . $rows['Email']. " ";
echo "<br>";
}
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "lecture11";
$con = mysqli_connect($host, $user, $password, $db);
if( mysqli_connect_errno()){
echo 'Connection Error:' . mysqli_connect_error();
}
//$Fname=$Lname=$Email="";
//perform only when the user click on insert
if(isset($_POST['insert'])){
// store user inputs in variables
$Fname=$_POST['Fname'];
$lname=$_POST['Lname'];
$Email=$_POST['Email'];
// step 3# create an insert sql query
$sql = "INSERT INTO students (Fname, Lname, Email)
VALUES ('$Fname', '$lname' ,'$Email')";
// step 4# preform the insert Query in the database by using mysqli_query()
$result=mysqli_query($con, $sql);
// step 5# verify the query (optional)
if ($result) {
echo "New record created successfully <br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Lecture 11</title>
</head>
<body>
<form method="post">
First name:
<input type="text" name="Fname">
Last name:
<input type="text" name="Lname">
Email:
<input type="text" name="Email">
<input type="submit" name="insert" value="INSERT" >
</form>
<table border="1">
<tr>
<th> ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Email </th>
</tr>
<?php
// step 3# create a Select sql query
$sql = "SELECT ID,Fname,Lname,Email FROM students";
// step 4# preform the insert Query in the database using mysqli_query()
$result= mysqli_query($con, $sql);
// step a & b
while ($rows = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $rows['ID'] . "</td>";
echo "<td>" . $rows['Fname'] . "</td>";
echo "<td>" . $rows['Lname'] . "</td>";
echo "<td>" . $rows['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "lecture11";
$con = mysqli_connect($host, $user, $password, $db);
if( mysqli_connect_errno()){
echo 'Connection Error:' . mysqli_connect_error();
}
//$Fname=$Lname=$Email="";
//perform only when the user click on insert
if(isset($_POST['insert'])){
// store user inputs in variables
$Fname=$_POST['Fname'];
$lname=$_POST['Lname'];
$Email=$_POST['Email'];
// step 3# create an insert sql query
$sql = "INSERT INTO students (Fname, Lname, Email)
VALUES ('$Fname', '$lname' ,'$Email')";
// step 4# preform the insert Query in the database by using mysqli_query()
$result=mysqli_query($con, $sql);
// step 5# verify the query (optional)
if ($result) {
echo "New record created successfully <br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Lecture 11</title>
</head>
<body>
<form method="post">
First name:
<input type="text" name="Fname">
Last name:
<input type="text" name="Lname">
Email:
<input type="text" name="Email">
<input type="submit" name="insert" value="INSERT" >
</form>
<table border="1">
<tr>
<th> ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Email </th>
</tr>
<?php
// step 3# create a Select sql query
$sql = "SELECT ID,Fname,Lname,Email FROM students";
// step 4# preform the insert Query in the database using mysqli_query()
$result= mysqli_query($con, $sql);
// step a & b
while ($rows = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $rows['ID'] . "</td>";
echo "<td>" . $rows['Fname'] . "</td>";
echo "<td>" . $rows['Lname'] . "</td>";
echo "<td>" . $rows['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
//perform only when the user click on update
if(isset($_POST['update'])){
$sql="UPDATE students SET Fname='$_POST[fname]' , Lname= '$_POST[lname]' ,Email='$_POST[email]' WHERE ID=$_POST[id]";
$result=mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "updated";
}else{
echo "wasn't able to make the required update";
}
}
//perform only when the user click on Delete
if(isset($_POST['Delete'])){
$sql="DELETE FROM students WHERE ID=$_POST[id]";
$result=mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "DELETED";
}else{
echo "wasn't able to make the required delete";
}
}
//perform only when the user click on insert
if(isset($_POST['insert'])){
// store user inputs in variables
$Fname=$_POST['Fname'];
$lname=$_POST['Lname'];
$Email=$_POST['Email'];
// step 3# create an insert sql query
$sql = "INSERT INTO students (Fname, Lname, Email)
VALUES ('$Fname', '$lname' ,'$Email')";
// step 4# preform the insert Query in the database by using mysqli_query()
$result=mysqli_query($con, $sql);
// verify the query (optional)
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "lecture7";
$con = mysqli_connect($host, $user, $password, $db);
if( mysqli_connect_errno()){
echo 'Connection Error:' . mysqli_connect_error();
}
//perform only when the user click on update
if(isset($_POST['update'])){
$sql="UPDATE students SET Fname='$_POST[fname]' , Lname= '$_POST[lname]' ,Email='$_POST[email]' WHERE ID=$_POST[id]";
$result=mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "updated";
}else{
echo "wasn't able to make the required update";
}
}
//perform only when the user click on Delete
if(isset($_POST['Delete'])){
$sql="DELETE FROM students WHERE ID=$_POST[id]";
$result=mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "DELETED";
}else{
echo "wasn't able to make the required delete";
}
}
//perform only when the user click on insert
if(isset($_POST['insert'])){
// store user inputs in variables
$Fname=$_POST['Fname'];
$lname=$_POST['Lname'];
$Email=$_POST['Email'];
// step 3# create an insert sql query
$sql = "INSERT INTO students (Fname, Lname, Email)
VALUES ('$Fname', '$lname' ,'$Email')";
// step 4# preform the insert Query in the database by using mysqli_query()
$result=mysqli_query($con, $sql);
// step 5# verify the query (optional)
if ($result) {
echo "New record created successfully <br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Lecture 11</title>
</head>
<body>
<table border="0">
<tr>
<th> ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Email </th>
</tr>
<?php
// step 3# create a Select sql query
$sql = "SELECT ID,Fname,Lname,Email FROM students";
// step 4# preform the insert Query in the database using mysqli_query()
$result= mysqli_query($con, $sql);
// step a & b
while ($rows = mysqli_fetch_array($result)){
echo"<form method ='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='id' value=" . $rows['ID'] . " readonly></td>";
echo "<td>" . "<input type='text' name='fname' value= " . $rows['Fname'] . "></td>";
echo "<td>" . "<input type='text' name='lname' value= ". $rows['Lname'] . "></td>";
echo "<td>" . "<input type='email' name='email' value=" . $rows['Email'] . "></td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . "</td>";
echo "<td>" . "<input type='submit' name='Delete' value='Delete'>" . "</td>";
echo "</tr>";
echo "</form>";
}
echo "<form method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' readonly>". "</td>" ;
echo "<td>" . "<input type='text' name='Fname'>". "</td>" ;
echo "<td>" . "<input type='text' name='Lname'>". "</td>" ;
echo "<td>" . "<input type='email' name='Email'>". "</td>" ;
echo "<td colspan=2>" . "<input type='submit' name='insert' value='INSERT' >". "</td>" ;
echo "</tr>";
echo "</form>";
echo "</table>";
?>
</body>
</html>