1

I'm not being able to insert any data to MySQL database due to this error that I'm getting after inserting and submitting the form.

Warning: Undefined array key "image" in C:\xampp\htdocs\eCommerce\admin\items.php on line 304

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\eCommerce\admin\items.php on line 304

Warning: Undefined array key "image" in C:\xampp\htdocs\eCommerce\admin\items.php on line 305

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\eCommerce\admin\items.php on line 305

this is some code of items.php:

elseif ($action == 'Add') {

        ?>

<h1 class="text-center">Add New Item</h1>
<div class="container">
    <form class="form-horizontal" action="?action=Insert" method="POST" enctype="multipart/form-data">
 <div class="form-group form-group-lg">
        <label for="image" class="col-sm-2 control-label">Item Image</label>
        <div class="col-sm-10 col-md-6">
            <input type="file" name="image" id="image" class="form-control" required="required">

        </div>
    </div>


    <!-- End Tags Field -->
    <!-- Start Submit Field -->
    <div class="form-group form-group-lg">
        <div class="col-sm-offset-2 col-sm-10">
            <input type="submit" value="Add Item" class="btn btn-primary btn-sm" />
        </div>
    </div>
    <!-- End Submit Field -->
    </form>

</div>

<?php

    } elseif ($action == 'Insert') {

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            echo "<h1 class='text-center'>Insert Item</h1>";
            echo "<div class='container'>";

            // Get Variables From The Form

            $name = $_POST['name'];
            $desc = $_POST['description'];
            $price = $_POST['price'];
            $country = $_POST['country'];
            $status = $_POST['status'];
            $member = $_POST['member'];
            $cat = $_POST['category'];
            $tags = $_POST['tags'];
line 304:   $image = $_FILES['image']['name'];
line 305:   $tmpImage = $_FILES['image']['tmp_name'];

            // Validate The Form

            $formErrors = array();

            if (empty($name)) {
                $formErrors[] = 'Name Can\'t be <strong>Empty</strong>';
            }

            if (empty($desc)) {
                $formErrors[] = 'Description Can\'t be <strong>Empty</strong>';
            }

            if (empty($price)) {
                $formErrors[] = 'Price Can\'t be <strong>Empty</strong>';
            }

            if (empty($country)) {
                $formErrors[] = 'Country Can\'t be <strong>Empty</strong>';
            }

            if ($status == 0) {
                $formErrors[] = 'You Must Choose the <strong>Status</strong>';
            }

            if ($member == 0) {
                $formErrors[] = 'You Must Choose the <strong>Member</strong>';
            }

            if ($cat == 0) {
                $formErrors[] = 'You Must Choose the <strong>Category</strong>';
            }

            if (empty($image)) {
                $formErrors[] = 'You Must Choose the <strong>Image</strong>';
            }

            // Loop Into Errors Array And Echo It

            foreach ($formErrors as $error) {
                echo '<div class="alert alert-danger">' . $error . '</div>';
            }

            // Check If There's No Error Proceed The Update Operation

            if (empty($formErrors)) {

                move_uploaded_file($tmpImage, "./items_images/$image");

                // Insert Userinfo In Database

                $stmt = $pdo->prepare("INSERT INTO

        items(itemName, Description, Price, Country_Made, Status, Add_Date, Cat_ID, User_ID, tags, image)

        VALUES(:zname, :zdesc, :zprice, :zcountry, :zstatus, now(), :zcat, :zmember, :ztags, :zimage)");

                $stmt->execute(array(

                    'zname' => $name,
                    'zdesc' => $desc,
                    'zprice' => $price,
                    'zcountry' => $country,
                    'zstatus' => $status,
                    'zcat' => $cat,
                    'zmember' => $member,
                    'ztags' => $tags,
                    'zimage' => $image
                ));

                // Echo Success Message

                $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' Record Inserted</div>';

                redirectHome($theMsg, 'back');

            }

        } else {

            echo "<div class='container'>";

            $theMsg = '<div class="alert alert-danger">Sorry You Cant Browse This Page Directly</div>';

            redirectHome($theMsg);

            echo "</div>";

        }

        echo "</div>";

I don't know how to solve this error.

D K
  • 19
  • 4
  • 2
    Does this answer your question? ["Notice: Undefined Index", or "Warning: Undefined array key"](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836) – shingo Apr 07 '23 at 09:37

0 Answers0