I am pretty new to PHP and I am obtaining a blank page while submitting a form. Also no data is being added onto the database. Each time an expense record is added, the values for amount, date, id(user id) and cat_id should be added into the myexpense
table.
Database
HTML
<form action="newexpense.php" method="post">
<label for="amount"><i class="fas fa-money-bill"></i> <strong>Amount (Rs)</strong></label><br>
<input type="number" id="amount" name="amount" placeholder="Input the amount spent"><br><br>
<p><i class="fas fa-clipboard-list"></i> <strong>Please select the category:</strong></p>
<input type="radio" id="cat1" name="category" value="Car">
<label for="cat1">Car</label><br>
<input type="radio" id="cat2" name="category" value="Gifts">
<label for="cat2">Gifts</label><br>
<input type="radio" id="cat3" name="category" value="Groceries">
<label for="cat3">Groceries</label><br>
<input type="radio" id="cat4" name="category" value="House">
<label for="cat4">House</label><br>
<input type="radio" id="cat5" name="category" value="Medicine">
<label for="cat5">Medicine</label><br>
<input type="radio" id="cat6" name="category" value="Travel">
<label for="cat6">Travel</label><br>
<input type="radio" id="cat7" name="category" value="Other">
<label for="cat7">Other</label><br><br>
<label for="date"><i class="fas fa-calendar-alt"></i> <strong>Date</strong></label><br>
<input type="date" id="date" name="date"><br><br><br>
<p class="b-option">
<input type="submit" name="submit" value="Add Expense" class="loginbtn">
</p>
</form>
PHP
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])) {
include "db.php";
if (isset($_POST["submit"])) {
$amount = $_POST["amount"];
$date = $_POST["date"];
$category = $_POST["category"];
$cat_id = "";
switch($category) {
case "Car":
return $cat_id = 1;
break;
case "Gifts":
return $cat_id = 2;
break;
case "Groceries":
return $cat_id = 3;
break;
}
$sql = "INSERT INTO myexpense (amount, date, id, cat_id)
VALUES ('".$amount."', '".$date."', '".$_SESSION['id']."', '".$cat_id."')";
if ($conn->query($sql) === TRUE) {
header("location: newexpensesuccess.php");
}else {
$message = "Error: " . $sql . "<br>" . $conn->error;
}
}
?>