Initially, I was going to have 3 separate files for creating, update and delete but to cut down the number of files I decided to make it all in 1 file. The delete action was working when the href was going to a page that was only handling the delete process. The issue is my $_GET doesn't seem to be working in the processor when it shares a file with create and update. Currently, if you click on Delete it creates a null record in the table so it's going to the create function.
viewCountries.php
$CountryID = $row['COUNTRYID'];
<tr>
<td><?php echo $row['COUNTRYNAME']; ?></td>
<td><?php echo $row['GDP']; ?></td>
<td><a href="editCountry.php?CountryID=<?php echo $CountryID; ?>" class="btn btn-warning">Edit</a></td>
<td><a href="processor/countryProcessor.php?CountryID=<?php echo $CountryID; ?>" class="btn btn-danger">Delete</a></td>
countryProcessor.php
<?php
require('../scripts/x_connect.php');
if(isset($_POST)) {
// Create Country
if(!isset($_POST['CountryID'])) {
if(isset($_POST['CountryName'])) {
$CountryName = $_POST['CountryName'];
}
if(isset($_POST['Gross'])){
$Gross = $_POST['Gross'];
}
$stmt = oci_parse($conn, "INSERT INTO COUNTRY (COUNTRYNAME, GDP) VALUES (:CountryName, :GDP)");
oci_bind_by_name($stmt, ":CountryName", $CountryName);
oci_bind_by_name($stmt, ":GDP", $Gross);
oci_execute($stmt);
$Affected = oci_num_rows($stmt);
oci_commit($conn);
oci_free_statement($stmt);
oci_close($conn);
// echo $Gross;
// echo $CountryName;
if(count($Affected) > 0){
header("Location: ../viewCountries.php?Success=$CountryName has been created!");
} else {
header("Location: ../viewCountries.php?Danger=$CountryName hasn't been created!");
}
// Update Country
} else {
if(isset($_POST['CountryID'])) {
$CountryID = $_POST['CountryID'];
}
if(isset($_POST['CountryName'])) {
$CountryName = $_POST['CountryName'];
}
if(isset($_POST['Gross'])){
$Gross = $_POST['Gross'];
}
$stmt = oci_parse($conn, "UPDATE COUNTRY SET COUNTRYNAME = :CountryName, GDP = :GDP WHERE COUNTRYID = :CountryID");
oci_bind_by_name($stmt, ":CountryID", $CountryID);
oci_bind_by_name($stmt, ":CountryName", $CountryName);
oci_bind_by_name($stmt, ":GDP", $Gross);
oci_execute($stmt);
$Affected = oci_num_rows($stmt);
oci_commit($conn);
oci_free_statement($stmt);
oci_close($conn);
// echo "CountryID" . ' ' . $CountryID . "<br>";
// echo "GDP" . ' ' . $Gross . "<br>";
// echo "Country Name" . ' ' . $CountryName . "<br>";
// echo "Rows Affected" . ' ' . $Affected;
if(count($Affected) > 0){
header("Location: ../viewCountries.php?Success=$CountryName has been updated!");
} else {
header("Location: ../viewCountries.php?Danger=$CountryName hasn't been updated!");
}
}
} else {
// Delete Country
if(isset($_GET['CountryID'])) {
$CountryID = $_GET['CountryID'];
$stmt = oci_parse($conn, "DELETE FROM COUNTRY WHERE COUNTRYID = :CountryID");
ocibindbyname($stmt, ":CountryID", $CountryID);
oci_execute($stmt);
$Affected = oci_num_rows($stmt);
oci_commit($conn);
oci_free_statement($stmt);
oci_close($conn);
if(count($Affected) > 0){
header("Location: ../viewCountries.php?Success=Country has been deleted!");
} else {
header("Location: ../viewCountries.php?Danger=Country hasn't been deleted!");
}
}
}
?>