简体   繁体   中英

Notice: Undefined index: image in D:\wamp\www\onlinesShop\newitem.php on line 21

I tr to upload image in my phpMYadmin with php and msqli But Got the mentioned error so practically everything will insert except the image.I Think every thing is good don't know where is the problem please help:(

here is the php:

<?php
        if(isset($_POST['submit'])){
        $Name=$_POST['Name'];
        $Desc=$_POST['Desc'];
        $Image=$_FILES['image']['tmp_name']; //get error in this line

        echo $_FILES['image']['error'];//get error in this line


        $sql="INSERT INTO `items`(`Name`,`Description`, `image`) 
        VALUES('$Name','$Desc','$Image')";


        if(mysqli_query($con,$sql)){ 
        echo "new record";
        }
        else{echo"Wrong";}
        mysqli_close($con);
        }
        ?>

the html:

<form action="http://localhost/onlinesShop/newitem.php" method="POST" > 
            <table  id='table_admin'>
                <tbody>
                  <tr>
                    <td width="116" height="50" align="left">Name</td>
                    <td width="466"><input name="Name" type="text" id="Name" title="Name" maxlength="200"></td>
                  </tr>
                  <tr>
                    <td height="290">Description</td>
                    <td><textarea name="Desc" cols="40" rows="15" id="textarea"></textarea></td>
                  </tr>
                  <tr>
                    <td>Image</td>
                    <td width="80"><input type="file" name="image" id="fileField" ></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="submit" id="submit" value="Upload"></td>
                  </tr>
                </tbody>
        </form>
            </table>
        </div>

Always make sure you set the <form> with enctype="multipart/form-data" when you're dealing with file uploads:

<form action="newitem.php" method="post" enctype="multipart/form-data">

Then after that, as usual, continue processing thru $_FILES :

if(isset($_POST['submit'])){
    $Name=$_POST['Name'];
    $Desc=$_POST['Desc'];
    $Image=$_FILES['image']['tmp_name'];

    $sql = 'INSERT INTO items (Name, Description, image) VALUES(?, ?, ?)';
    $insert = $con->prepare($sql);
    $insert->bind_param('sss', $Name, $Desc, $Image);
    $insert->execute();

    if($insert->num_rows > 0) {
        echo 'insert okay!';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM