PDO Error: " Invalid parameter number: parameter was not defined" - php

I am trying to use a simple MySQL insert query with the parameters in array form. It keeps telling me the number of parameters are wrong. I have tried the following, all producing the same error:
$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, :room, :name, :message, :time, :color)');
$stmt3->execute(array(':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));
and
$stmt3 = $link->prepare('INSERT INTO messages VALUES(:null, :room, :name, :message, :time, :color)');
$stmt3->execute(array(':null' => null, ':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));
as well as declaring the columns specifically to avoid the null insert:
$stmt3 = $link->prepare('INSERT INTO messages (room, name, message, time, color) VALUES(:room, :name, :message, :time, :color)');
$stmt3->execute(array(':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));
This is my first time using PDO (I normally use mysqli, but my current shared host does not have the mysqlnd plugin, preventing me from using prepare(), so any insight from that point of view is appreciated.

The problem - and you will kick yourself - is with :color.
The array key for the value you are passing for that marker when calling execute() is named :color:. Remove the trailing : (I'm guessing this was just a typo anyway).
$stmt3->execute(array(
':room' => $Clean['room'],
':name' => $Clean['name'],
':message' => $Clean['message'],
':time' => $time,
':color' => $Clean['color'],
));

I might be wrong here, but as far as I know you need to do this:
$stmt3->bindParam(':room', $Clean['room']);
$stmt3->bindParam(':name', $Clean['name']);
//and so on
But as a personal preference, I've always done it like this
$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, ?, ?, ?, ?, ?)');
$stmt3->execute(array($Clean['room'], $Clean['name'], $Clean['message'], $time, $Clean['color']))

Related

PHP PDO INSERT INTO not inserting

I have a problem while inserting datas in MySQL database with PDO.
I have no error, it just seems to didn't have inserted datas in the table when I select them after execute the code with MySQL in terminal.
I've tried solutions in answers on stackoverflow, like wraping name and description in backticks, but it's still not working
Here is my sql columns :
Here is the code :
$query = $pdo->prepare("INSERT INTO cities (`name`, departement, province, lat, long, `description`, poi_id) VALUES(`:name`, :departement, :province, :lat, :long, `:description`, :poi_id)");
$query = $query->execute([
"name" => $name,
"departement" => $code_dpt,
"province" => (int)$provinces[$departements[$code_dpt]],
"lat" => $lat,
"long" => $long,
"description" => $description,
"poi_id" => (int)$poi_id
]);
$query = $pdo->prepare("INSERT INTO cities (`name`, departement, province, lat, long, `description`, poi_id) VALUES(:name, :departement, :province, :lat, :long, :description, :poi_id)");
$query->execute([
':name' => $name,
':departement' => $code_dpt,
':province' => (int)$provinces[$departements[$code_dpt]],
':lat' => $lat,
':long' => $long,
':description' => $description,
':poi_id' => (int)$poi_id
]);
use placeholder in execute function.
I've found the solution.
First, I've enabled errors like this :
$pdo = new PDO('mysql:host=localhost;dbname=bpfmgr', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
I was using a property named long for longitude, but it's a MySQL reserved keyword, so it mades errors. I've added backticks to wrap long and that's working.

Insert into a database a single string value of a randomized array

I have this code:
$possible_pics = array(
'red-pfp' => 'teem-pfp-red.svg',
'pink-pfp' => 'teem-pfp-pink.svg',
'blue-pfp' => 'teem-pfp-blue.svg',
'green-pfp' => 'teem-pfp-green.svg',
'purple-pfp' => 'teem-pfp-purple.svg',
'yellow-pfp' => 'teem-pfp-yellow.svg',
'orange-pfp' => 'teem-pfp-orange.svg',
);
shuffle($possible_pics);
echo reset($possible_pics);
The result of it, I want to insert it into a database this way:
$sentence = $connection->prepare("
INSERT INTO users (id, user, pass, email, profile_pic) VALUES (:id, :user, :password, :email, :profile_pic)
");
$sentence->execute(array(
':id' => $user_id,
':user' => $user,
':password' => $password,
':email' => $email,
':profile_pic' => $possible_pics
));
At this point I have already connected to the database, I'm just doing the SQL code.
As you can see, I am inserting the values into the database through an array, and in the part of :profile_pic, I am saying that I want to insert the result of the first code I added to my question, where I am shuffling the array and it only brings us 1 value. The problem here is that when I run this, it shows this:
Notice: Array to string conversion on line 73
Why is this happening and how can I make so it inserts the value of the randomized array? Where I do that, it works perfectly, and it returns effectively only 1 value. I can't do an implode() because it marks, unexpected implode().
In resume, how can I insert into a database the returned value of the randomized array I showed at the beginning of my question?
Just pick the output of reset function to a variable and use it in your insert statement.
shuffle($possible_pics);
$shuffledPic = reset($possible_pics);
...
$sentence->execute(array(
':id' => $user_id,
':user' => $user,
':password' => $password,
':email' => $email,
':profile_pic' => $shuffledPic
));

SQL Query how to call enum?

How would i go about trying to call enum value from SQL for my SQL Query below if someone could assist and point me in the right direction it would be great.
$insertLogSQL = $odb -> prepare("INSERT INTO `logs`
VALUES (NULL, :user, :ip, :port, :time, :method, UNIX_TIMESTAMP(), :stopped, :server)");
$insertLogSQL -> execute(array(
':user' => $_SESSION['username'],
':ip' => $host,
':port' => $port,
':time' => $time,
':method' => $method,
':stopped' => 0,
':server' => )
);
My code work fine i'm just trying to implement server rotation but wouldn't know how to go about enum ?

Insert into mysql with prepared statment - not working

I am using php-login.net projectfiles for a tiny webapp. I just tried to connect to m db and insert some stuff but am keep getting this strange error which i cannot figure out. What am missing in this code
$query = $db->prepare('INSERT INTO cours (category, title, description, places, price) VALUES (:category, :title, :description, :places, :price');
$query->execute(array(':category' => $category, ':title' => $title, ':decription' => $decription, ':places' => $places, ':price' => $price));
You had a typo in description (was decription) also a ) was missing after :price
$query = $db->prepare('INSERT INTO cours (category, title, description, places, price) VALUES (:category, :title, :description, :places, :price)');
$query->execute(array(':category' => $category, ':title' => $title, ':description' => $description, ':places' => $places, ':price' => $price));
Can you try now and tell us if it works?

PDO how to treat param variable that can be empty

I have a prepared statement and param with variables, but it's possible that some of these variables may be empty and it gives me an error.. how to treat the code to SKIP the empty param?
CODE:
$stmt = $conn->prepare("insert into account (accountID, firstName, imageURL, sex, cat1) VALUES (:accountID, :firstName, :sourcefilename, :sex, :cat1)");
$stmt->execute(array(
':accountID' => $accountID,
':firstName' => $first_name,
':sourcefilename' => $sourcefilename,
':sex' => $gender,
':cat1' => $cat1));

Categories