0

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.

wes
  • 379
  • 3
  • 15

1 Answers1

1

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'].

  • This is what I have under the PHP code, but its never getting there (if()) because there is a undefined variable. if you want I can past the code somewhere. – wes Nov 19 '16 at 22:25
  • Where do you have `$_FILES`? All I see in your question is `$_POST`. – Joe C Nov 19 '16 at 22:34
  • @JoeC, as I said above I have this after the above code. here is the full code: http://pastebin.com/vKqH1ejG – wes Nov 19 '16 at 22:43
  • You are not binding params here. Change "value1" to ":value1" and so on. Checkout [pdo statment docs](http://php.net/manual/en/pdostatement.execute.php). – Radosław Halicki Nov 19 '16 at 23:37
  • Thx for your comment Radoslaw, but sadly this was not full awnser, nothing is appearing in the database – wes Nov 20 '16 at 00:50
  • Did you figured out what's wrong? I've edited my answer a bit. I hope this was the problem. If not, write about any errors you still recive. – Radosław Halicki Nov 20 '16 at 19:36