0

i am getting this error when i process this form

Notice: Undefined index: image in C:\xampp2\htdocs\trail\auction_upload.php on line 10

Notice: Undefined index: image in C:\xampp2\htdocs\trail\auction_upload.php on line 13

<?php
session_start();
$_SESSION['message']="";

$mysqli = new mysqli('localhost', 'root', '','auction');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$item_name = $mysqli->real_escape_string($_POST['item_name']);
$item_description = $mysqli->real_escape_string($_POST['item_description']);
$item_image = $mysqli->real_escape_string('images/item_img/'.$_FILES['image']['name']);

//make sure file is of image type
if (preg_match("!image!", $_FILES['image']['type'])) {
    if (copy($_FILES['image']['tmp_name'], $item_image)) {
        $_SESSION['item_name'] = $item_name;
        $_SESSION['item_description'] = $item_description;  
        $_SESSION['image'] = $item_image;
        $sql = "INSERT INTO items (item_name, item_image, item_description)
                VALUES('$item_name', '$image','$item_description')";
        if ($mysqli->query($sql) == true) {
            $_SESSION['message'] = "Item Upload Successful!";
        } else {
            $_SESSION['message'] = "file upload failed";
        }

    }
    else{
        $_SESSION['message'] = "file copying failed";
}
    }
     else {
    $_SESSION['message'] = "please upload gif, jpg, png";
}

}

?> <html> <head> <title>Upload item</title> <link rel="StyleSheet" href="Bootstrap/css/bootstrap.main.css"> <link rel="StyleSheet" href="Bootstrap/css/bootstrap.min.css"> <link rel="StyleSheet" href="style.css"> </head> <body> <div> <form class="form-horizontal" role="form" action="auction_upload.php" method="POST"> <div class=" form-group"> <label class="control-label col-sm-2">Item Name:</label> <div class="col-sm-8"> <INPUT type="text" class="form-control" name="item_name" required/> </div> </div> <div class="form-group"> <label class="control-label col-sm-2">Item Image:</label> <div class="col-sm-8"> <INPUT type="file" class="form-control" name="image" accept="image/*" required/> </div> </div> <div class="form-group"> <label class="control-label col-sm-2">Item Description:</label> <div class="col-sm-8"> <textarea class="form-control" name="item_description" required>
2
Contributors
2
Replies
38
Views
3 Months
Discussion Span
Last Post by ernest_5
0

You are sending the form to its own page. This is not usually a good idea as page refresh ends up resubmitting the form
You should send to a form handler. Anyway when the page loads for the first time you are telling it to check the files variable, which obviously does not have an image element yet as nothing has been sent. If you insist on this approach you need to use a conditional structure to ascertain whether it exists or not in the first place.

Edited by diafol

0

Figured it ou! Turns out out i didn't include enctype="multipart/form-data" int the form tag. The problem now is that i can't access/display image from the database. Here is the updated code.

<?php
//for image upload

session_start();
$_SESSION['message']="";

$mysqli = new mysqli('localhost', 'root', '','auction');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$item_name = $mysqli->real_escape_string($_POST['item_name']);
$item_description = $mysqli->real_escape_string($_POST['item_description']);
$item_image_path = $mysqli->real_escape_string('Images/item_img/'.$_FILES['item_image']['name']);

//make sure file is of image type
if (preg_match("!image!", $_FILES['item_image']['type'])) {
    if (copy($_FILES['item_image']['tmp_name'], $item_image_path)) {
        $_SESSION['item_name'] = $item_name;
        $_SESSION['item_description'] = $item_description;  
        $_SESSION['item_image'] = $item_image_path;
        $sql = "INSERT INTO items (item_name, item_image_path, item_description)
                VALUES('$item_name', '$item_image_path','$item_description')";
        if ($mysqli->query($sql) == true) {
            $_SESSION['message'] = "Item Upload Successful!";
        } else {
            $_SESSION['message'] = "file upload failed";
        }

    }
    else{
        $_SESSION['message'] = "file copying failed";
}
    }
     else {
    $_SESSION['message'] = "please upload gif, jpg, png";
}

}

?>

<html>
<head>
    <title>Upload item</title>
    <link rel="StyleSheet" href="Bootstrap/css/bootstrap.main.css">
    <link rel="StyleSheet" href="Bootstrap/css/bootstrap.min.css">
    <link rel="StyleSheet" href="style.css">

</head>
<body>
    <div>
        <div>
            <?php
                $mysqli = new mysqli('localhost','root','','auction');
                $sql = "SELECT * FROM items ";
                $result = mysqli_query($mysqli, $sql);
                while ($row = mysqli_fetch_array($result)) {

                    echo "<img src='Images\item_img/".$row['item_image']."'>";
                    echo "<p>".$row['item_name']."</p>";

                }
            ?>
        </div>
        <form class="form-horizontal" role="form" action="auction_upload.php" method="POST" enctype="multipart/form-data">
            <h1><? = $_SESSION['message'];?></h1>
            <div class=" form-group">
        <label class="control-label col-sm-2">Item Name:</label>
        <div class="col-sm-8">
            <INPUT type="text" class="form-control" name="item_name" required/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2">Item Image:</label>
        <div class="col-sm-8">
            <INPUT type="file" class="form-control" name="item_image" accept="image/*" required/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2">Item Description:</label>
        <div class="col-sm-8">
            <textarea class="form-control" name="item_description" required></textarea>
        </div>
    </div>

   <div class="form-group"> 
  <div class="col-sm-offset-2 col-sm-8"> 
     <button type="submit" class="btn btn-default" name="upload">Upload</button>
  </div> 
   </div> 
        </form>
    </div>
</body>
</html>
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.