找到你要的答案

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

Q:注意:未定义的指标:图像D:\ \ \ \ newitem.php onlinesshop WWW服务器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>

我TR上传图像在我的phpMyAdmin PHP和MSQLI但有上述错误,几乎将除了形象。我认为每件事都是好的不知道哪里出了问题请帮助:(

这里是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);
        }
        ?>

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>
answer1: 回答1:

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!';
    }
}

始终确保你设置的<;形式>;用enctype =“multipart/form-data”当你处理文件上传:

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

然后,像往常一样,继续处理通过_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!';
    }
}
php  mysql