0

I am making a simple inventory management web application in PHP. I made a simple form to add an item in the inventory with it's name, shelf life, date of delivery and expiry date. However, when I try to add it and save it to the database, I get the following error:

Fatal error: Uncaught mysqli_sql_exception: Unknown column '' in 'field list' in C:\xampp\htdocs\ikaworld\Inventory\add.php:12 Stack trace: #0 C:\xampp\htdocs\ikaworld\Inventory\add.php(12): mysqli_query(Object(mysqli), 'INSERT INTO inv...') #1 {main} thrown in C:\xampp\htdocs\ikaworld\Inventory\add.php on line 12

I have googled this error but no solutions work for this.

Here is my DB connection:

<?php

$DB_HOST='localhost';
$DB_USER='root';
$DB_PASSWORD='';
$DB_NAME='ikaworld';

// Make the connection:
$conn = mysqli_connect ($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);

My inventory page:

<?php
// Requires DB document for connection //
require 'resources/database/mysqli_connect.php';
session_start();
$rank = 0;
// If session is not empty, assigns variables and runs the query.

if(!empty($_SESSION['id'])){
    // If user is logged, creates variables with values // 
    $id = $_SESSION['id'];
    $result = mysqli_query($conn, "SELECT * FROM users WHERE id = '$id'");
    // fetches the row with the current user ID and stores details in variable //
    $row = mysqli_fetch_assoc($result);
    $rank = $row['rank'];
}
else{
    // If user is not logged, this error shows //
    echo '<script>alert("Please login to access");location="login.php";</script>';
}
if(isset($_POST["addButton"])){
    // Gets form data and stores in variable + cleans it //
    $itemName = mysqli_real_escape_string($conn, $_POST['itemName']);
    $shelfLife= mysqli_real_escape_string($conn, $_POST['shelfLife']);
    $dod = mysqli_real_escape_string($conn, $_POST['dod']);
    $expiry = mysqli_real_escape_string($conn, $_POST['expiry']);
}

?>

<html>
    <head>
        <title>Inventory</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <link rel="stylesheet" href="resources/stylesheet/stylesheet.css">
        <link rel="stylesheet" href="resources/stylesheet/register.css">
        
    </head>
    <header>
        <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
            <a href="./index.php" class="navbar-brand">IKAWORLD</a>
            <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarMenu">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item">
                        <a href="index.php" class="nav-link">Home</a>
                    </li>
                    <?php 
                    if($rank == 5){
                        echo
                            "<li class='nav-item'>
                                <a href='./admin.php' class='nav-link'>Admin</a>
                            </li>";
                    }
                    ?>
                    <?php 
                    if(empty($_SESSION["id"])){
                        echo 
                            "<li class='nav-item'>
                                <a href='login.php' class='nav-link'>Login</a>
                            </li>
                            <li class='nav-item'>
                                <a href='register.php' class='nav-link'>Register</a>
                            </li>";
                    } else{
                        echo
                            "
                            <li class='nav-item'>
                                <a href='inventory.php' class='nav-link'>Inventory</a>
                            </li>
                            <li class='nav-item'>
                                <a href='profile.php' class='nav-link'>Profile</a>
                            </li>
                            <li class='nav-item'>
                                <a href='logout.php' class='nav-link'>Logout</a>
                            </li>";
                    }
                    ?>
                </ul>
            </div>
        </nav>
    </header>
    

    <body>
        <div class="content-wrapper">
            <br>
            <h1 class="subheading">Add Item</h1>
            <form method="post" action="inventory/add.php">
                <table>
                    <th>Item Name</th>
                    <th>Shelf Life</th>
                    <th>Delivery Date</th>
                    <th>Expiry Date</th>
                    <tr>
                        <!-- The php code inside placeholder displays error message [Empty by default] -->
                        <td><input type="text" name="itemName" placeholder="" ></td>
                        <td><input type="text" name="shelfLife" placeholder="" ></td>
                        <td><input type="date" name="dod" placeholder="" ></td>
                        <td><input type="date" name="expiry" placeholder="" ></td>
                    </tr>
                </table>
                <button type="submit" name="addButton">Add Result</button>
            </form>
        </div>
    
    
    </body>

</html>

Add.php (the script which tries to save it to the DB)

<?php
ob_start();
// Requires DB document for connection //
require '../resources/database/mysqli_connect.php';
// Requires editsquads page to use the code here //
require_once '../inventory.php';

// Checks if any fields are empty //
if(!empty($_POST["itemName"]&& $_POST["shelfLife"] && $_POST["dod"] && $_POST["expiry"])){
    $query = "INSERT INTO inventory (`id`, `item`, `shelf_life`, `dod`,  `expiry`) VALUES(``, `$itemName`, `$shelfLife`, `$dod`, `$expiry`)";
    // Runs the query to add //
    mysqli_query($conn, $query);
    // Redirects user //
    header("Location: ../inventory.php");
}


ob_end_flush();
?>



Researched online but no solution works.

Nikita
  • 682
  • 2
  • 13
  • 1
    The values for the insert should not be in backticks. BUT you should be using prepared statements instead of building statements like this - https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Nigel Ren Jul 01 '23 at 06:07

0 Answers0