Queen'S University Belfast MSC Web Dev

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lab

Queen's University
Belfast

MSc Web Dev


Introduction
This lab continues on from the Oscars lab on updating data that you previously have completed. You
should have something like this…

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.

Let's focus on the update process.

• Create a new folder called 'includes' at the root directory…

• Add a new file into the 'includes' folder called functions.php…

web dev 1
• Add the update code function…

<?php

function update_movie($update_id, $update_name){

include("../conn.php");

$update_id = $conn->real_escape_string($update_id);
$update_name = $conn->real_escape_string($update_name);

$updateSQL="UPDATE oscars SET movie_name='$update_name' WHERE id='$update_id' ";

$result = $conn->query($updateSQL);

if(!$result){
echo $conn->error;
}else{
echo "<p>Update successful <a href='editmovies.php'>back to list</a></p> ";
}

?>

• Save your file


• Upload the 'includes' folder to the root directory of your web server…

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'];
}

echo "<form method='POST' action='edit.php'>


<fieldset>
<label for='nameField'>Name</label>
<input type='text' value='$name_data' name='nameField'>
<input type='hidden' value='$id_data' name='idField'>
<input class='button-primary' type='submit' value='update'>
</fieldset>
</form>";
}
?>
</div>

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…

echo "<form method='POST' action='edit.php'>

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']);

• Save the file

web dev 3
• Upload the file into the 'admin' directory…

• Test the site so the update still works…

web dev 4
Add the Winner Update

• Add the following code to the edit.php…

<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{

echo "<form method='POST' action='edit.php'>


<fieldset>
<label for='nameField'>Name</label>
<input type='text' value='$name_data' name='nameField'>
<label for='winField'>Winner</label>
<select name='winField'>
<option value='0'>No</option>
<option value='1'>Yes</option>
</select>
<input type='hidden' value='$id_data' name='idField'>
<input class='button-primary' type='submit' value='update'>
</fieldset>
</form>";
}
?>
</div>
• Save the file
• Upload the edit.php to your web server…

• Add the following code to the functions.php source…

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);

$updateSQL = "UPDATE oscars SET


movie_name='$update_name',
winner='$update_win'
WHERE id='$update_id' ";

$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…

• Test the site…

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.

• Open the index.php file in VS Code


• Add the following code…

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{

$win = "<img height='30px'


src='https://www.flaticon.com/svg/static/icons/svg/1828/1828804.svg'>";

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

You might also like