duanpanyang1962 2016-11-19 22:09
浏览 37

[PHP] [MYSQL] $ _POST未定义索引且无法插入数据库[重复]

Q1: I'm new to programming and have no idea what I'm doing wrong. I'm getting the following error:

Notice: Undefined index: pimage in [..]

this is the code:

<form method="post" enctype="multipart/form-data">
    <label for="pimage">photo</label>
    <input type="file" name="pimage" id="fileToUpload"/><br />
    <input type="submit" name="psubmit" value="register" />
</form>

and this is my PHP:

if (isset($_POST['psubmit'])) {
$image = $_POST['pimage'];

Q2: After the above code the form should be placed in a database, I don't get any errors but I don't see anything appearing in the database. Here is the PHP code:

if (isset($_POST['psubmit'])) {
    include 'opendb.php';
    $stmt = $dbh->prepare("INSERT INTO new_products(Title, Price, CID,
Categorie, Sub-categorie, Description, Shipping) VALUES(:value1, :value2,
:value3, :value4, :value5, :value6, :value7)");
    $image = $_POST['pimage'];
    $values = array(
        'value1' => $_POST['ptitle'],
        'value2' => $_POST['pprice'],
        'value3' => $_POST['pcid'],
        'value4' => $_POST['pcat'],
        'value5' => $_POST['psubcat'],
        'value6' => $_POST['pdescription'],
        'value7' => $_POST['pshipping'], );

    $stmt->execute($values);
    $stmt->errorInfo();
}

great that this question is marked as a duplicated, but I don't see the awnser in the given link. I know what I have to set $_POST['pimage'] and as far as I know I did. I don't want to be the person that you need to feed the information but please give me the text in the given link so this piece of code is done.

</div>
  • 写回答

1条回答 默认 最新

  • douhushen3241 2016-11-19 22:14
    关注

    Just check out $_FILES global variable and small tutorial about uploading files here.

    Your file name is here $_FILES["pimage"]["name"]. But you need to copy your file from temporary php path to yours server file folder with function move_uploaded_file($_FILES["pimage"]["tmp_name"], $target_file).

    You have error in params biding, try this out:

    if (isset($_POST['psubmit'])) {
        include 'opendb.php';
        $stmt = $dbh->prepare("INSERT INTO new_products(Title, Price, CID,
        Categorie, Sub-categorie, Description, Shipping) VALUES(:value1, :value2,
        :value3, :value4, :value5, :value6, :value7)");
    
        $values = array(
            ':value1' => $_POST['ptitle'],
            ':value2' => $_POST['pprice'],
            ':value3' => $_POST['pcid'],
            ':value4' => $_POST['pcat'],
            ':value5' => $_POST['psubcat'],
            ':value6' => $_POST['pdescription'],
            ':value7' => $_POST['pshipping']);
    
        $stmt->execute($values);
        $stmt->errorInfo();
    }
    

    And there is no such variable as $_POST['pimage']. There is only $_FILES['pimage'].

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题