PDO:Fatal error: Uncaught exception 'PDOException'

This is the error that I get

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY093]: Invalid parameter number: parameter was not defined’ in E:\Xamp\htdocs\php12\insert.php:11 Stack trace: #0 E:\Xamp\htdocs\php12\insert.php(11): PDOStatement->execute() #1 {main} thrown in E:\Xamp\htdocs\php12\insert.php

on line 11

what am I missing?

You cannot use umlauts in the placeholder names.

And it is not recommended to use them in the variable names too.

could you explain a bit more? I’m new to this
what do you mean with umlauts?

Umlaut

You can use only basic latin characters, numbers and underscore.

I changed it to

<?php

include(“connection.php”);

//prepare query
$query = $con->prepare(“INSERT INTO login (Namn, Losenord) VALUES (:Namn, :Losenord)”);

$query->bindParam(‘:Namn’, $Namn);
$query->bindParam(‘:Losenord’,$Losenord);
$Namn = ; //data here
$Losenord = ; //data here

$query->execute();

echo “Information has been saved to the database.”;

$con = null; //close database connection
?>

Now I get this error:
Parse error: syntax error, unexpected ‘;’ in E:\Xamp\htdocs\php12\insert.php on line 10

Then the problem is the same as well.

To overcome this issue you may wish to avoid named placeholders at all

$Namn = ; //data here
$Losenord = ; //data here
$query = $con->prepare("INSERT INTO login (Namn, Losenord) VALUES (?, ?)");
$query->execute([$Namn,$Losenord]);

Besides, you may be still getting an error, but a different one.

I keep getting the
syntax error, unexpected ‘;’ in E:\Xamp\htdocs\php12\insert.php on line 10

what should I put here? after “=”?
$Namn = ; //data here
$Losenord = ; //data here

It was your own code. I have no idea where do you get these variables or what to put there. You are supposed to have these variables already set. Do you?

Well teacher gave me these code and I was supposed to make them work.

well could you tell me how I can fix the error, I just need it to save to database

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY093]: Invalid parameter number: parameter was not defined’ in E:\Xamp\htdocs\php12\insert.php:12 Stack trace: #0 E:\Xamp\htdocs\php12\insert.php(12): PDOStatement->execute(Array) #1 {main} thrown in E:\Xamp\htdocs\php12\insert.php on line 12

he used

$Column1 = ; //data here
$Column2 = ; //data here

which I changed to
$Namn = ; //data here
$Losenord = ; //data here

Don’t you need to give these two variables values? Right now they are undefined. Maybe your teacher expected you to make up the values.

yeah, but do I need these

$Namn = ; //data here
$Losenord = ; //data here

To upload username and password to database?

or could I just use:

<?php

include(“connection.php”);

//prepare query
$query = $con->prepare(“INSERT INTO login (Namn, Losenord) VALUES (:Namn, :Losenord)”);

$query->bindParam(‘:Namn’, $Namn);
$query->bindParam(‘:Losenord’,$Losenord);

$query->execute([$Namn,$Losenord]);

echo “Information has been saved to the database.”;

$con = null; //close database connection
?>

You need to change those to something like:

$Namn = 'Your Name';
$Losenord = 'Your password;
<?php

include(“connection.php”);

//prepare query
$query = $con->prepare(“INSERT INTO login (Namn, Losenord) VALUES (:Namn, :Losenord)”);

$query->bindParam(‘:Namn’, $Namn);
$query->bindParam(‘:Losenord’,$Losenord);
$Namn = ‘pelle’;
$Losenord = ‘123’;

$query->execute([$Namn,$Losenord]);

echo “Information has been saved to the database.”;

$con = null; //close database connection
?>


thats my code and when Im trying to make an account i get this error

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY093]: Invalid parameter number: parameter was not defined’ in E:\Xamp\htdocs\php12\insert.php:13 Stack trace: #0 E:\Xamp\htdocs\php12\insert.php(13): PDOStatement->execute(Array) #1 {main} thrown in E:\Xamp\htdocs\php12\insert.php on line 13

You need to set the values before you use them. With the code the way you have it the query will fail because you haven’t set the values yet.

Set the values THEN use them to access the database.

You don’t need to set the values before you use them in this code snippet. This is how bindParam () actually works.

OP now seems to be using bindParam as well as passing in an array of values for the named parameters - would that work? And wouldn’t they need to define the parameter names within the array? Surely the name and password should be coming from the form, not being hard-coded?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.