1

I'm new to coding with PHP, and in trying to create a to-do list I have come undone after creating a file called 'add.php' which is supposed to add new data to the table. The main file, index.php, calls the table elements ok and displays them on the browser, but I cannot add new items.

The code in add.php is here:

<?php

require_once 'app/init.php';

if(isset($_POST['name'])) {
    $name = trim($POST['name']);

    if(!empty($name)) {
        $addedQuery = $db->prepare("
            INSERT INTO items (name, user, done, created) 
            VALUES (:name, :user, 0, NOW() )
                ");

        $addedQuery->execute([
            'name' => $name,
            'user' => $_SESSION['user_id']
            ]);
    }
}

header('location: index.php');




?> 

If it helps, I am using PHP 5.5.12 and MySQL 5.6.17, on WampServer 2.5

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Karen
  • 65
  • 10
  • [believe me, it's a syntax error... **click**...](http://php.net/manual/en/function.error-reporting.php) – Funk Forty Niner Sep 16 '15 at 02:44
  • it is either a problem with the `require_once 'app/init.php';` or it is the `POST`. Because that is all I don't have that you do, and I get thru fine, data inserted, when I fake the `POST`. Nothing else changed at all. Well, except doing a start_session and cramming that var in. But besides all of that ! – Drew Sep 16 '15 at 03:12

2 Answers2

5

OK... and before anyone gets the misconception about being obligated to use the colon as a placeholder in the array, is a myth.

  • It is perfectly legal/valid syntax in PDO. (consult footnote).

  • The colon however, does need to be in the VALUES().

The real problem here is with this $POST, and is missing its underscore for it $_POST.

Error reporting would have thrown you an undefined variable notice.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Plus, make sure the session array is set/not empty, and that the session was started. That isn't shown/indicated in your post.

Also make sure that your form does use a POST method and that your input holds the name attribute for the input.

I.e.: <input type="text" name="name">

Check for errors in PDO also:

Your connection method is also unclear, so make sure you are indeed using PDO to connect with.

  • Different MySQL APIs do not intermix.

Other references:


Footnote:

As stated in comments and being a nice find on the part of VolkerK:

"it is stored internally with the leading colon, see"

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    smart-ass-mode on: "is perfectly legal/valid", yet it is stored internally _with_ the leading colon, see https://github.com/php/php-src/blob/master/ext/pdo/pdo_stmt.c#L366 ;-) – VolkerK Sep 16 '15 at 04:12
  • @VolkerK Nice find. I will add it to my answer, if you don't mind. *cheers* – Funk Forty Niner Sep 16 '15 at 04:13
  • I was looking because I suspected it to be the other way round - but the data is data :) – VolkerK Sep 16 '15 at 04:18
  • @VolkerK I've known for some time that the colon wasn't required in the array, and for the life of me, never did find the documentation for it. What you have found/provided, now takes the guesswork out of it for future readers to the question, *thank you*. :) – Funk Forty Niner Sep 16 '15 at 04:20
  • woah drive by dv. care to share? I doubt it, they never do. – Funk Forty Niner Sep 16 '15 at 04:23
  • 1
    I cannot believe that I was that stupid and missed the missing underscore. I salute you sir! And tanks for clearing up the colon myth for me. – 0xGiddi Sep 16 '15 at 04:38
  • @user3211152 TBH, that took me a second or third glance seeing it buried like that and bunched up against that `trim()` function. I guess my days of being a text editor a long time ago and taking this strange class in H.S. about correctional text paid off lol - *Cheers* ;-) and you're welcome. – Funk Forty Niner Sep 16 '15 at 04:41
  • Thank you, the missing underscore solved it! – Karen Sep 16 '15 at 19:25
  • @Karen You're most welcome Karen, *cheers* – Funk Forty Niner Sep 16 '15 at 19:26
-2

When passing named parameters in PDO execute method you must pass then as an array with the name prefixed with a colon (:name not name).

And when passing indexed parameters (using ? in the prepare method) then you don't associate a key at all in he execute method.

so try:

$addedQuery->execute([ ':name' => $name, ':user' => $_SESSION['user_id'] ]);

and make sure that $_SESSION['user_id'] is set properly.

EDIT: Did you turn on php errors in he php.ini?

0xGiddi
  • 404
  • 2
  • 12
  • err.... no, not required and an undocumented feature. `'name' => $name,` perfectly valid. – Funk Forty Niner Sep 16 '15 at 02:52
  • http://www.php.net/manual/en/pdostatement.bindparam.php Under the section parameter it clearly states that **this will be a parameter name of the form :name. ** – 0xGiddi Sep 16 '15 at 02:56
  • Thanks for the lesson, but I don't need it; I know this already and what I already said as a fact. ;-) anyway, wait for the OP then. Oh, and I did say "undocumented". – Funk Forty Niner Sep 16 '15 at 02:57
  • those downvotes you got, one of those **isn't** mine, in case you may be wondering as I just got a dv now. If that was yours, it's uncalled for and should be retracted. You got those because you were wrong. – Funk Forty Niner Sep 16 '15 at 04:24
  • 1
    One downvote was by me, because the answer is wrong; at the very, very least in the context of the question. `you must`, no, it makes no difference. So, it's a coding style advise at best. – VolkerK Sep 16 '15 at 04:28
  • I did not downvote you fred, this is not an competition and we are all here to learn including me :) . And everyone has the right to downvote an answer that is wrong. – 0xGiddi Sep 16 '15 at 04:31
  • @user3211152 fair enough. As I said, I did say "if". Well, somebody out there didn't agree with me. What can you do; it comes with the territory I guess. – Funk Forty Niner Sep 16 '15 at 04:32