0

I am sorry to bother you with such a newbie question, and thank you for taking the time to go over it and answer it.

function dbaddusr($username, $email, $password){
        try{
            $conn = new PDO(CONNECTDATA);
            $stmt = $conn->prepare("INSERT INTO 'users' ('username', 'email', 'password') VALUES (:username, :email, :password)");
            $pass = crypt($password);
            $result = $stmt->execute(array("username" => $username, "email" => $email, "password" => $pass));
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
            return false;
        }
    }

Problem is, $result is always false. (I discovered this by some simple var_dump statements inside the try block. I am very new to this and your help on fixing it is highly appreciated.

  • you're otputting the error message - what does it say? What do you output the error message for if you don't read it? – zerkms May 17 '13 at 12:19
  • It doesnt trigger an error. The $result is false but there is no error message. THat is the confusing part. –  May 17 '13 at 12:21
  • Either you forgot to supply username and password or concatenated them into literal string with commas which would make no sense. Either way you will find the proper code from the link above – Your Common Sense May 17 '13 at 12:21
  • @aayushdagra: you're performing `echo`. Put `exit;` right after it and see – zerkms May 17 '13 at 12:21
  • I var_dumped result and discovered that its false, and ofcourse the database never gets appended with the new data. Sadly i never actually caught the PDOException.. –  May 17 '13 at 12:21
  • 1
    Also, You shouldn't connect every time you call a function. You'd kill your database server. – Your Common Sense May 17 '13 at 12:22
  • @zerk, i already tried that. I even put some code there and exit immediately afterwards, the exception was never even triggered. –  May 17 '13 at 12:22
  • @Common - I am very new and this is more of an experiment than anything else, if you know a better way then please tell me about it. –  May 17 '13 at 12:22
  • Duplicate of [PDO query fails but I can't see any errors. How to get an error message from PDO?](http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15990858) (corrected link) – Your Common Sense May 17 '13 at 12:31
  • @YourCommonSense why are you not providing duplicates as closevotes? I've seen you do that several times now. You got the necessary reputation to do so, so please do it. – Gordon May 20 '13 at 13:12

4 Answers4

3

Don't quote the column names, if you want, use the backticks `

INSERT INTO users (username, email, password) VALUES (:username, :email, :password)
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

Change quotes to backticks for table & column name in your query,

 $stmt = $conn->prepare("INSERT INTO `users` (`username`, `email`, `password`) VALUES      
        (:username, :email, :password)");
Rikesh
  • 26,156
  • 14
  • 79
  • 87
0

You are passing $pass in your array and your function accepts $password Check your error messages to get specific details and you will find the problem.

randomizer
  • 1,619
  • 3
  • 15
  • 31
-1

A non-bloated version with all useless and wrong code cleaned.

function dbaddusr($username, $email, $password){
    global $conn;
    $sql  = "INSERT INTO users (username, email, password) VALUES (?,?,?)";
    $stmt = $conn->prepare($sql);
    $pass = crypt($password);
    $stmt->execute(array($username, $email, $pass));
}

You have to connect ONCE per application, and then use that single connection all the way.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Yeah, why "bloat" code with nasty stuff like exception handling and encapsulation. Just make everything global and let the browser see your PHP errors... – Steven Moseley May 17 '13 at 12:27
  • The code I cleaned out was actually letting the browser to see your PHP errors. – Your Common Sense May 17 '13 at 12:37
  • 1
    While you have `global` in your answer, my down-vote will remain. Your example perpetuates some of the worst habits among PHP developers. – Steven Moseley May 17 '13 at 12:41
  • @StevenMoseley - I would wrap the function call in a try catch, not have it within the method – AlexP May 17 '13 at 12:41
  • @AlexP - You think a DAO should throw raw PDO exceptions? I'm not saying it shouldn't throw an exception, but definitely not an unhandled one. – Steven Moseley May 17 '13 at 12:43