-1

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 my database structure

HTML

<form action="newexpense.php" method="post">

    <label for="amount"><i class="fas fa-money-bill"></i>&nbsp;<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>&nbsp;<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>&nbsp;<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;
    }
      
  }
  
?>
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
Arvin
  • 11
  • 2
  • 1
    You are open for [SQL injection](https://stackoverflow.com/questions/601300/what-is-sql-injection) – DarkBee Nov 24 '21 at 10:19

2 Answers2

1

When you do a return in your switch statement, no code below it will be executed, so you query won't execute too. Remove the return statements before the variable assignments. Also the last break is redundant.

switch($category) {
    case "Car":
        $cat_id = 1;
        break;
    case "Gifts":
        $cat_id = 2;
        break;
    case "Groceries":
        $cat_id = 3;
}

But I don't like switch statements so much. Imagine your list of categories becomes longer, than you would get a really long switch block.

My approach would be to use an array with the categories and iterate over them in a loop.

$catMap = [ 'Car' => 1, 'Gifts' => 2, 'Groceries' => 3 ];

foreach ( $catMap as $name => $id ) {
    if ( $name === $category ) {
        $cat_id = $id;
        break;
    }
}

With this method the code is shorter, but also more dynamic. You only have to modify the array to add more categories.

jrswgtr
  • 2,287
  • 8
  • 23
  • 49
0

In your script you have a switch case statement. And the case will be true you assign the variable $cat_id = xxxx; Then you set return. The return statement aborts the script at this point. Use instead break; And that's the reason why you don't get any output. The correct use of the switch-case command is:

switch($var) {
  case '1': 
       $var = 'something';
       break;
  case '2': 
       $var = 'something';
       break;
  default: 
       // do something to avoid error or something else.
       $var = 'default Value';
       // or
       exit(); // because nothing matched

KrassVerpeilt
  • 437
  • 3
  • 10