Professional Documents
Culture Documents
Queen'S University Belfast MSC Web Dev
Queen'S University Belfast MSC Web Dev
Queen'S University Belfast MSC Web Dev
Queen's University
Belfast
Functions
The PHP scripts are working fine. The software design pattern can be implemented to separate the
view and functionality. We can use PHP custom defined functions.
web dev 1
• Add the update code function…
<?php
include("../conn.php");
$update_id = $conn->real_escape_string($update_id);
$update_name = $conn->real_escape_string($update_name);
$result = $conn->query($updateSQL);
if(!$result){
echo $conn->error;
}else{
echo "<p>Update successful <a href='editmovies.php'>back to list</a></p> ";
}
?>
You need to trigger the function when the form submit button is submitted.
web dev 2
• Edit the edit.php source…
<div class="container">
<h2>Edit Movie</h2>
<?php
if(isset($_POST['nameField'])){
include("../includes/functions.php");
update_movie($_POST['idField'], $_POST['nameField']);
}else{
include("../conn.php");
$id_data = $conn->real_escape_string($_GET["editid"]);
$readmovie = "SELECT * FROM oscars WHERE id=$id_data";
$result = $conn->query($readmovie);
if(!$result){
echo $conn -> error;
}
while($row = $result->fetch_assoc()){
$name_data = $row['movie_name'];
}
With the above script you are now navigating to the same as the page using a POST rather than
travelling to the update.php script…
The script asks if the <form> element name 'nameField' was posted by the user…
if(isset($_POST['nameField'])){
You then include the functions file and call the update function by sending it two arguments data
that are sent through the form…
include("../includes/functions.php");
update_movie($_POST['idField'], $_POST['nameField']);
web dev 3
• Upload the file into the 'admin' directory…
web dev 4
Add the Winner Update
<div class="container">
<h2>Edit Movie</h2>
<?php
if(isset($_POST['nameField'])){
include("../includes/functions.php");
update_movie($_POST['idField'], $_POST['nameField'], $_POST["winField"]);
}else{
web dev 5
function update_movie($update_id, $update_name, $update_win){
include("../conn.php");
$update_id = $conn->real_escape_string($update_id);
$update_name = $conn->real_escape_string($update_name);
$update_win = $conn->real_escape_string($update_win);
$result = $conn->query($updateSQL);
if(!$result){
echo $conn->error;
}else{
echo "<p>Update successful <a href='editmovies.php'>back to list</a></p>";
}
}
• Save and upload the functions.php…
web dev 6
Change Data
Now let's just add some script that allows the front-end of the site to display an Oscar image inside a
1 or 0. It just uses a simple if condition.
while($row = $result->fetch_assoc()){
$name = $row['movie_name'];
$yr = $row["year"];
$win = $row["winner"];
if($win=='1'){
$win = "<img height='30px'
src='https://www.flaticon.com/svg/static/icons/svg/206/206982.svg'>";
}else{
echo "<tr>
<td>$name</td>
<td>$yr</td>
<td>$win</td>
</tr>";
}
• Save and upload the index.php file…
web dev 7
• Test the site
End of lab.
web dev 8