【问题标题】:Notice: Undefined index: image in D:\wamp\www\onlinesShop\newitem.php on line 21注意:未定义索引:第 21 行 D:\wamp\www\onlinesShop\newitem.php 中的图像
【发布时间】:2015-07-12 03:45:00
【问题描述】:

我想用 php 和 msqli 在我的 phpMYadmin 中上传图片,但是遇到了上面提到的错误,所以几乎所有东西都会插入,除了图片。我认为每件事都很好,不知道问题出在哪里,请帮忙:(

这里是 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>

【问题讨论】:

  • 需要在&lt;form&gt;内添加enctype="multipart/form-data"。检查@Ghost 答案它会工作
  • 不,我再次收到错误@NarendraSisodia

标签: php mysql


【解决方案1】:

在处理文件上传时,请务必将&lt;form&gt; 设置为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!';
    }
}

【讨论】:

  • 它可以工作@Ghost,但所有信息都会显示,除了 phpmyAdmin 中的图像,它显示为 D:wamp mpphpE757.tmp 而不是显示目录你能帮我吗?@Ghost
  • @LiLi 你需要先保存文件,使用move_uploaded_file 然后保存你保存它的路径
猜你喜欢
  • 2016-10-09
  • 2012-12-13
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多