I am trying to make a CRUD app for my business, I have little knowledge with PHP. I followed a guide that has proven to be successful until I got to the update portion.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database= "replog_test";
//create connection
$connection = new mysqli($servername, $username, $password, $database);
$id = "";
$service_dept="";
$po="";
$customer="";
$pcs="";
$equipment="";
$date_rec="";
$problem="";
$warr="";
$date_ship="";
$errorMessage = "";
$successMessage = "";
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
//GET method: Show the data of the client
if (!isset($_GET["id"])) {
header("location: /novasvm/index.php");
exit;
}
$id = $_GET["id"];
// read the row of the selected client from the database table
$sql = "SELECT * FROM replog WHERE id=$id";
$result = $connection->query($sql);
$row = $result->fetch_assoc();
if (!$row) {
header("location: /novasvm/index.php");
exit;
}
$service_dept = $row["service_dept"];
$po = $row["po"];
$customer = $row["customer"];
$pcs = $row["pcs"];
$equipment = $row["equipment"];
$date_rec = $row["date_rec"];
$problem = $row["problem"];
$warr = $row["warr"];
$date_ship = $row["date_ship"];
}
else {
// POST method: Update the data of the client
$service_dept = $_POST["service_dept"];
$po = $_POST["po"];
$customer = $_POST["customer"];
$pcs = $_POST["pcs"];
$equipment = $_POST["equipment"];
$date_rec = $_POST["date_rec"];
$problem = $_POST["problem"];
$warr = $_POST["warr"];
$date_ship = $_POST["date_ship"];
do {
if ( empty($service_dept) || empty($po) || empty($customer) || empty($pcs) || empty($equipment) || empty($date_rec) || empty($problem) || empty($warr)) {
$errorMessage = "All fields except DATE SHIPPED are required";
break;
}
$sql = "UPDATE replog " .
"SET service_dept = '$service_dept', po = '$po', customer = '$customer', pcs = '$pcs', equipment = '$equipment', date_rec = '$date_rec', problem = '$problem', warr = '$warr', date_ship = '$date_ship' " .
"WHERE id = '$id'";
$result = $connection->query($sql);
if (!$result) {
$errorMessage = "Invalid Query: " . $connection->error;
break;
}
$successMessage = "Client Updated Correctly";
header("location: /novasvm/general/replog.php");
exit;
} while(false);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../style.css" type="text/css">
<link rel="stylesheet" href="../reset.css" type="text/css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<title>Repair Log</title>
</head>
<body>
<div>
<h2>New Data</h2>
<?php
if ( !empty($errorMessage)) {
echo "
<div>
<strong>$errorMessage</strong>
</div>
";
}
?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div>
<label>Service Department</label>
<div>
<input type="text" name="service_dept" value="<?php echo $service_dept; ?>">
</div>
</div>
<div>
<label>PO Number</label>
<div>
<input type="text" name="po" value="<?php echo $po; ?>">
</div>
</div>
<div>
<label>Customer</label>
<div>
<input type="text" name="customer" value="<?php echo $customer; ?>">
</div>
</div>
<div>
<label>Pieces</label>
<div>
<select name="pcs" id="">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
</div>
<div>
<label>Equipment</label>
<div>
<input type="text" name="equipment" value="<?php echo $equipment; ?>">
</div>
</div>
<div>
<label>Date Received</label>
<div>
<input type="date" name="date_rec" value="<?php echo $date_rec; ?>">
</div>
</div>
<div>
<label>Problem / Serial No.</label>
<div>
<input type="text" name="problem" value="<?php echo $problem; ?>">
</div>
</div>
<div>
<label>Warranty</label>
<div>
<select name="warr" id="">
<option value="" selected="selected"></option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div>
<label>Date Shipped</label>
<div>
<input type="date" name="date_ship" value="<?php echo $date_ship; ?>">
</div>
</div>
<?php
if (!empty($successMessage)) {
echo "
<div>
<strong>$successMessage</strong>
</div>
";
}
?>
<div>
<button type="submit">Submit</button>
</div>
<div>
<a href="/novasvm/general/replog.php" role="button">Cancel</a>
</div>
</form>
</div>
</body>
</html>
Once I submit the data it never actually updates on the xampp server nor the webpage. It stays exactly the same.
Expected update of database information not correctly functioning.
You're not setting the $id
in your actual else
branch for the UPDATE...
statement. Try it like this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database= "replog_test";
//create connection
$connection = new mysqli($servername, $username, $password, $database);
$id = "";
$service_dept="";
$po="";
$customer="";
$pcs="";
$equipment="";
$date_rec="";
$problem="";
$warr="";
$date_ship="";
$errorMessage = "";
$successMessage = "";
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
//GET method: Show the data of the client
if (!isset($_GET["id"])) {
header("location: /novasvm/index.php");
exit;
}
$id = $_GET["id"];
// read the row of the selected client from the database table
$sql = "SELECT * FROM replog WHERE id=$id";
$result = $connection->query($sql);
$row = $result->fetch_assoc();
if (!$row) {
header("location: /novasvm/index.php");
exit;
}
$service_dept = $row["service_dept"];
$po = $row["po"];
$customer = $row["customer"];
$pcs = $row["pcs"];
$equipment = $row["equipment"];
$date_rec = $row["date_rec"];
$problem = $row["problem"];
$warr = $row["warr"];
$date_ship = $row["date_ship"];
}
else {
// POST method: Update the data of the client
$service_dept = $_POST["service_dept"];
$po = $_POST["po"];
$customer = $_POST["customer"];
$pcs = $_POST["pcs"];
$equipment = $_POST["equipment"];
$date_rec = $_POST["date_rec"];
$problem = $_POST["problem"];
$warr = $_POST["warr"];
$date_ship = $_POST["date_ship"];
$id = $_POST["id"]; // <----------
do {
if ( empty($service_dept) || empty($po) || empty($customer) || empty($pcs) || empty($equipment) || empty($date_rec) || empty($problem) || empty($warr)) {
$errorMessage = "All fields except DATE SHIPPED are required";
break;
}
$sql = "UPDATE replog " .
"SET service_dept = '$service_dept', po = '$po', customer = '$customer', pcs = '$pcs', equipment = '$equipment', date_rec = '$date_rec', problem = '$problem', warr = '$warr', date_ship = '$date_ship' " .
"WHERE id = '$id'";
$result = $connection->query($sql);
if (!$result) {
$errorMessage = "Invalid Query: " . $connection->error;
break;
}
$successMessage = "Client Updated Correctly";
header("location: /novasvm/general/replog.php");
exit;
} while(false);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../style.css" type="text/css">
<link rel="stylesheet" href="../reset.css" type="text/css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<title>Repair Log</title>
</head>
<body>
<div>
<h2>New Data</h2>
<?php
if ( !empty($errorMessage)) {
echo "
<div>
<strong>$errorMessage</strong>
</div>
";
}
?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div>
<label>Service Department</label>
<div>
<input type="text" name="service_dept" value="<?php echo $service_dept; ?>">
</div>
</div>
<div>
<label>PO Number</label>
<div>
<input type="text" name="po" value="<?php echo $po; ?>">
</div>
</div>
<div>
<label>Customer</label>
<div>
<input type="text" name="customer" value="<?php echo $customer; ?>">
</div>
</div>
<div>
<label>Pieces</label>
<div>
<select name="pcs" id="">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
</div>
<div>
<label>Equipment</label>
<div>
<input type="text" name="equipment" value="<?php echo $equipment; ?>">
</div>
</div>
<div>
<label>Date Received</label>
<div>
<input type="date" name="date_rec" value="<?php echo $date_rec; ?>">
</div>
</div>
<div>
<label>Problem / Serial No.</label>
<div>
<input type="text" name="problem" value="<?php echo $problem; ?>">
</div>
</div>
<div>
<label>Warranty</label>
<div>
<select name="warr" id="">
<option value="" selected="selected"></option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div>
<label>Date Shipped</label>
<div>
<input type="date" name="date_ship" value="<?php echo $date_ship; ?>">
</div>
</div>
<?php
if (!empty($successMessage)) {
echo "
<div>
<strong>$successMessage</strong>
</div>
";
}
?>
<div>
<button type="submit">Submit</button>
</div>
<div>
<a href="/novasvm/general/replog.php" role="button">Cancel</a>
</div>
</form>
</div>
</body>
</html>