Bryce
Bryce

Reputation: 383

PHP: Issue inserting data to MySQL from simple html form

I am relatively new to the PHP / MYSQL world (and programming in general) so apologies in advance for any ignorance on my part.

I have been following a YouTube tutorial from PHPAcademy detailing how to create a simple HTML form and submit data via PHP & MySQLi. The video also teaches how to perform a SELECT * statement and display the entire table in an HTML table.

My issue is that I am unable to post or add the information from the form to the MySQL database. Below is my index.php file & database structure. Any help you can provide is greatly appreciated. Also, I have a connect.php script that initiates the MySQL connection and a security.php script that ensures only UTF-8 text can be inserted into the database. I can provide both of those upon request.

Thank you!

<?php 
error_reporting(0);
require 'db/connect.php';
require 'security.php';

$records = array();

if(!empty($_POST)) {
    if(isset($_POST['items_item'], $_POST['items_item_location'], $_POST['items_notes'], $_POST['items_quantity'])) {

        $items_item          = trim($_POST['items_item']);
        $items_item_location = trim($_POST['items_item_location']);
        $items_notes         = trim($_POST['items_notes']);
        $items_quantity      = trim($_POST['items_quantity']);

        if(!empty($items_item) && !empty($items_item_location) && !empty($items_notes) && !empty($items_quantity)) {
            $insert = $db->prepare("INSERT INTO items (items_item, items_item_location, items_notes, items_quantity) VALUES (?, ?, ?, ?)");
            $insert->bind_param('ssss', $items_item, $items_item_location, $items_notes, $items_quantity);

            if($insert->execute()) {
                header('Location: index.php');
                die();
            }
        }
    }
}

if($results = $db->query("SELECT * FROM items")) {
    if($results->num_rows) {
        while($row = $results->fetch_object()){
            $records[] = $row;
        }
        $results->free();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Grocery list!</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet"> 
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/scripts.js"></script>
</head>

<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    Grocery Application <small>Created by Bryce</small>
                </h1>
            </div>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-lg-4 column">
            <form class="form-horizontal" action="" method='post'>
              <fieldset>
                <legend>Add grocery item</legend>
                <div class="form-group">
                  <label for="inputItem" class="col-lg-2 control-label">Grocery Item</label>
                  <div class="col-lg-10">
                    <input type="text" class="form-control" id="inputItem">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputLocation" class="col-lg-2 control-label">Location</label>
                  <div class="col-lg-10">
                    <input type="text" class="form-control" id="inputLocation">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputNotes" class="col-lg-2 control-label">Notes</label>
                  <div class="col-lg-10">
                    <textarea class="form-control" rows="3" id="inputNotes"></textarea>
                    <span class="help-block">Here you can enter notes about your item such as the quantity, number of units, or any other general information.</span>
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputLocation" class="col-lg-2 control-label">Quantity</label>
                  <div class="col-lg-10">
                    <input type="text" class="form-control" id="inputQuantity">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-lg-10 col-lg-offset-2">
                    <button type="submit" class="btn btn-primary">Submit</button>
                  </div>
                </div>
              </fieldset>
            </form>
        </div>
        <div class="col-md-8 column">
            <?php
            if(!count($records)){
                echo 'No records';                  
            } else {
            ?>
            <table class="table table-bordered table-striped">
                <tr>
                    <th>Item</th>
                    <th>Location</th>
                    <th>Notes</th>
                    <th>Quantity</th>
                </tr>
                <?php
                foreach($records as $r){
                ?>
                <tr>
                    <td><?php echo escape($r->items_item); ?></td>
                    <td><?php echo escape($r->items_item_location); ?></td>
                    <td><?php echo escape($r->items_notes); ?></td>
                    <td><?php echo escape($r->items_quantity); ?></td>
                </tr>
                <?php
                }
                ?>
            </table>
            <?php
            }
            ?>
        <br><br>
        </div>
    </div>
</div>
</body>
</html>

Database structure:

id (autoincremented, interger) | items_item (varchar 255) | items_item_location (varchar 255) | items_notes (text) | items_quantity (text)

Upvotes: 1

Views: 171

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Edit: This answer is a per your original post and not marking your edited question as an edit under the original.


None of your form elements contain a name attribute and are required when using POST.

Change your form elements to these, respectively:

<input name="items_item" type="text" class="form-control" id="inputItem">

<input name="items_item_location" type="text" class="form-control" id="inputLocation">

<textarea name="items_notes" class="form-control" rows="3" id="inputNotes"></textarea>

<input name="items_quantity" type="text" class="form-control" id="inputQuantity">

These ^, are to work in conjunction with:

$_POST['items_item']
$_POST['items_item_location']
$_POST['items_notes']
$_POST['items_quantity']

I hope you were not relying on an "id" alone, not for this anyway.

Plus using error_reporting(0); doesn't help, it suppresses possible errors.

Some of which would have been "Undefined index...".

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

Instead of doing:

if($insert->execute()) {
        header('Location: index.php');
        die();
    }

Use/replace with: (to check for possible errors)

if($insert->execute()) {

    header('Location: index.php');
    die();

            }
else{
    die('There was an error running the query [' . $db->error . ']');
    }

// rest of your code you have now

Upvotes: 5

Related Questions