1

Hej.

I´m new to PHP but strugling to learn. i have found out that this is the way to handle database connection. Have debugged the code but have one stubborn thing left. Cant seem to wrap my brain around this errorcode. Any pointer in simple way so even i understand. ;-)

I am surfing this pages: https://www.php.net/manual/en/pdostatement.bindparam.php

Error-message: Database connection establishedPDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\Test\dbtest.php:28 Stack trace: #0 C:\xampp\htdocs\Test\dbtest.php(28): PDOStatement->execute() #1 {main}

<?php
// Require needed classes
require_once('dbhandler.php');

// Create needed objects
$dbh = new DBHandler();

// Check if database connection established successfully
if ($dbh->getInstance() === null) {
    die("No database connection");
}

//$datetime = date("Y-m-d H:i:s");
$epost = 'svante@telia.com'; 
$namn = 'Svante';
$användarnamn = 'Poffe';
$lösenord = '1596';

try {
    $sql = "INSERT INTO users(epost, namn, användarnamn, lösenord) VALUES(:epost, :namn, :användarnamn, :lösenord)";

    $stmt = $dbh->getInstance()->prepare($sql);
    $stmt->bindParam(':epost', $epost, PDO::PARAM_STR);       
    $stmt->bindParam(':namn', $namn, PDO::PARAM_STR); 
    $stmt->bindParam(':användarnamn', $användarnamn, PDO::PARAM_STR);
    $stmt->bindParam(':lösenord', $lösenord, PDO::PARAM_STR);   

    $stmt->execute();
} 

catch(PDOException $e) {
    echo $e;
}
?>

/Svante

  • If you use characters without the accents does this error still occur? – Professor Abronsius Dec 24 '20 at 10:17
  • You meen here? $stmt->bindParam(':epost', $epost, PDO::PARAM_STR); $stmt->bindParam(:epost, $epost, PDO::PARAM_STR); This what the manual says: $sth->bindParam(':calories', $calories, PDO::PARAM_INT); – Svante Stenberg Dec 24 '20 at 10:20
  • Oh, sorry. made some changes after the message. I edit the main question. Its in row 28 offcourse. – Svante Stenberg Dec 24 '20 at 10:23
  • Please [edit] your question to: clarify title of question, more details or clarify of problem, improve text arragement, improve code formatting. See [ask] – Gander Dec 24 '20 at 10:32
  • Please surf [here](https://phpdelusions.net/pdo_examples/insert) and [here](https://phpdelusions.net/pdo_examples/connect_to_mysql) – Your Common Sense Dec 24 '20 at 13:23

1 Answers1

0

It might be worth trying a simplified version that omits the special characters from the PHP variable and the assigned placeholders.

<?php
    require_once('dbhandler.php');

    $dbh = new DBHandler();

    $e = 'svante@telia.com'; 
    $n = 'Svante';
    $a = 'Poffe';
    $l = '1596';

        $sql = "INSERT INTO users( `epost`, `namn`, `användarnamn`, `lösenord` ) VALUES( :e, :n, :a, :l )";

        $stmt = $dbh->getInstance()->prepare($sql);
        $stmt->bindParam(':e', $e, PDO::PARAM_STR);       
        $stmt->bindParam(':n', $n, PDO::PARAM_STR); 
        $stmt->bindParam(':a', $a, PDO::PARAM_STR);
        $stmt->bindParam(':l', $l, PDO::PARAM_STR);   

        $stmt->execute();
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Thank you Professor Abronsius. It work like a charm. I had that i mind but got carried away with other stuff. It was written in english before witch offcource is the "best effort" way but the variable "name" did hook up in the wrong way and i whent swedish instead. ;-) – Svante Stenberg Dec 24 '20 at 10:34
  • :-) good ~ glad that it helped – Professor Abronsius Dec 24 '20 at 10:40
  • Incidentally you can echo $e in the catch, let alone there shouldn't be no catch blocks at all – Your Common Sense Dec 24 '20 at 12:47
  • @YourCommonSense Can you? I'd have expected an error at that point about it being an object! And I'm confused by the double negative in your comment `shouldn't be no catch blocks` ~ do you mean there should be no catch blocks?? – Professor Abronsius Dec 24 '20 at 13:17
  • Yes, that's a double negative and there should be no catch blocks. And if you take a look at the question you answered, there is the output from the "impossible" `echo $e` provided – Your Common Sense Dec 24 '20 at 13:25
  • I see the fabled output - should, perhaps, have noticed that and put 2 and 2 together as they say. Is that why the -1? – Professor Abronsius Dec 24 '20 at 13:32
  • Yes. Usually I do not discuss the voting (and advise you to follow the suit) but my judgement is as follows: there is no positive value as the question is a duplicate and there is a better answer which is straight to the point, and there is some negative value due to misinformation. My bad temper contributed as well, though – Your Common Sense Dec 24 '20 at 14:21
  • OK - probably a fair approach to the voting debate - let's hope the `bad temper` can be relieved with some festive spirits. Happy Christmas – Professor Abronsius Dec 24 '20 at 14:25
  • I edited your answer to make it less harmful. Simply because the OP just asked another question where he keeps all the bad practices suggested here. – Your Common Sense Dec 26 '20 at 07:37