I got these codes from someone and it looks different from w3school course. What's the difference?
Connection:
<?php
/*
to avoid writing code to connect to the db in each file:
1-write the DB connexion code in a php file e.g: dbLogin.php
2-call that php file when needed with include e.g: include ('dbLogin.php') or include 'dbLogin.php'
There are also include_once, require and require_once which can be used instead of include
*/
$conn=mysqli_connect("localhost","root","","registration");
if (!$conn)
{
die("connexion failed".mysqli_connect_error());
}
?>
Construct list
<?php
// Construct School Name list from the database
$request="SELECT school_name FROM school ";
$result=mysqli_query($conn,$request);
$option='';
while($line=mysqli_fetch_assoc($result))
{
$option.= "<option value=\"".$line['school_name']."\">".$line['school_name']."</option>";
}
?>
Displaying list :
<?php
//Display the Students details(Student NCN First Name Last Name City Name School Name)
$req="select student_cin, first_name, last_name, city_name, school_name
from student, school, city
where student.postal_code=city.postal_code
and student.school_id=school.school_id";
$res = mysqli_query($conn,$req);
if(mysqli_num_rows($res)>0)
{
while ($line=mysqli_fetch_assoc($res))
{
echo"<tr> <td>".$line['student_cin'].
"</td><td>".$line['first_name']."</td><td>".
$line['last_name']."</td><td>".$line['city_name'].
"</td><td>".$line['school_name']."</td></tr>";
}
}
else
{
echo"no results found";
}
?>
Closing
<?php
mysqli_close($conn);
?>
ADD or EDIT
if(!empty($_POST['Student_ID']) AND !empty($_POST['FN']) AND !empty($_POST['LN']) AND !empty($_POST['city'])AND !empty($_POST['school']))
{
$FN=$_POST['FN'];
$Student_ID=$_POST['Student_ID'];
$LN=$_POST['LN'];
$city_n=$_POST['city'];
$school_n=$_POST['school'];
//We assume that the school_name and city_name entered in the form already exist in the tables school and city
//get the city postal code
$qc="SELECT postal_code FROM city WHERE city_name='$city_n'";
$rc=mysqli_query($conn,$qc);
if (mysqli_num_rows($rc) == 1)
{
$line=mysqli_fetch_assoc($rc);
$city_c=$line['postal_code'];
}
//get the school postal code
$qs="SELECT school_id FROM school WHERE school_name='$school_n'";
$rs=mysqli_query($conn,$qs);
if (mysqli_num_rows($rs) == 1)
{
$line=mysqli_fetch_assoc($rs);
$school_c=$line['school_id'];
}
// SQL query
$query="SELECT student_cin FROM student WHERE student_cin='$Student_ID'";
$result=mysqli_query($conn,$query);
if (mysqli_num_rows($result) == 0)
{
$req="INSERT INTO student VALUES('$Student_ID','$FN','$LN','$city_c','$school_c')";
$res=mysqli_query($conn,$req);
//if the insertion is done
echo "<br> res= ".$res." ";
if ($res)
{
echo "insertion successful";
}
else
echo "Problem when inserting student ". mysqli_error($conn);
}
else
{ $request="update student SET first_name='$FN',last_name='$LN',postal_code='$city_c', school_id='$school_c' WHERE student_cin='$Student_ID'";
$res=mysqli_query($conn,$request);
//if the update is done
if ($res)
{
echo "student updated";}
// else
else echo "problem updating student". mysqli_error($conn);
}
}
else
echo"<script type=\"text/javascript\"> alert('Please fill the form!')</script>";
header('location: index.php');
?>
REMOVE :
$sc=$_POST['SC_name'];
//get the school id
$reqid="select school_id from school where school_name='$sc'";
$resid=mysqli_query($conn,$reqid);
$line=mysqli_fetch_assoc($resid);
$sid=$line['school_id'];
echo 'school id'. $sid. '<br>';
//delete students from that school
$reqs="delete from student where school_id='$sid'";
$ress=mysqli_query($conn,$reqs);
if ($ress)
{
echo "student deleted";
}
// else
else echo "problem deleting student". mysqli_error($conn);
//delete school now
$req1="delete from school where school_name='$sc'";
$res1=mysqli_query($conn,$req1);
if ($res1)
{
echo "school deleted";}
// else
else echo "problem deleting school". mysqli_error($conn);
header('location: index.php');