I have an annoying problem with mdb2 while trying to insert empty values in an integer field.
$query = $conn->prepare("INSERT INTO enquiries
(type, idvenue, cname, fname, lname, phone, email, date, guests, budget, comments)
VALUES (:party, :venue, :cname, :fname, :lname, :phone, :email, :ddate, :guests, :budget, :options)",
array('integer', 'integer', 'text', 'text', 'text', 'text', 'text', 'datetime', 'integer', 'integer', 'text'),
MDB2_PREPARE_MANIP);
$result = $query->execute($_POST);
my $_POST array is:
[party] => 2
[venue] =>
[anyve] => checked
[cname] =>
[fname] => Javi
[lname] => Prieto
[phone] => 078087028492
[email] => ravenfewejp#gmfweail.com
[ddate] => 10/10/2011
[guests] => 10
[budget] => 15
[options] => My party!
in the moment that :venue is not empty, it works like a charm, even if :cname (text) is still empty, but when :venue is empty like this I get an undefined error. So I guess is happening only with integer fields, what am I missing?
try this before executing statement:
$_POST['venue'] = (int) $_POST['venue'];
I realised there's no problem with passing null values to integer fields in this kind of queries. The problem was I was sending one more element in $_POST when venue was empty, so the number of elements in $_POST was different than the number of placeholders.
Thanks for the help!
Related
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
));
Here is the return of AJAX, which display what value I am trying to store into database:
Here is what is really stored into database (58.00 is what I stored):
Here is the structure of table:
Here is the PHP:
$req = $bdd->prepare("INSERT INTO salon_histo(reference, designation, colour, size, type, price, qty, payment, date) VALUES(:reference, :designation, :colour, :size, :type, :price, :qty, :payment, NOW())");
$req->execute(array(
'reference' => $_POST['reference'],
'designation' => $_POST['designation'],
'colour' => $_POST['colour'],
'size' => $_POST['size'],
'type' => $_POST['type'],
'price' => floatval($price[0]),
'qty' => $_POST['soldQty'],
'payment' => $_POST['payment']
));
$req->closeCursor();
echo json_encode($price[0]);
How can MySQL store a data with 2 decimals at 0 when I am trying to store 58,33? I tried in PHP to use floatval and indeed the number becomes 58.
Price appears to be an array:
'price' => floatval($price[0]),
Possibly the decimal part has key 1?
'price' => (float) ($price[0] . '.' . $price[1]),
If that doesn't work, please var_dump($price) and paste me the result.
The other thing to check is wether PHP is formatting those numbers with a comma or not.
I'm working on this page where other users are added, when I want to insert a new user, this is what comes up:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21S01]: Insert value list does not match column list:
1136 Column count doesn't match value count at row 1' in /Applications/XAMPP/xamppfiles/htdocs/app/classes/class.users.php:96 Stack trace: #0
/Applications/XAMPP/xamppfiles/htdocs/app/classes/class.users.php(96): PDO->prepare('INSERT INTO use...') #1
/Applications/XAMPP/xamppfiles/htdocs/public/pages/nieuweklant.tpl(4): DeGier\Core\Users::addCus() #2
/Applications/XAMPP/xamppfiles/htdocs/app/classes/class.template.php(20): include('/Applications/X...') #3
/Applications/XAMPP/xamppfiles/htdocs/app/classes/class.template.php(48): DeGier\Core\Template::getPage('nieuweklant') #4
/Applications/XAMPP/xamppfiles/htdocs/index.php(11): DeGier\Core\Template::render('nieuweklant') #5 {main}
thrown in /Applications/XAMPP/xamppfiles/htdocs/app/classes/class.users.php on line 96
Normally, this pops up when I've missed a comma somewhere in the prepare statement, or if the statement isn't corresponding to my DB table, but this time, I believe that none of that has happened.
This is the PDO Statement:
$q = self::$connection->prepare('INSERT INTO users VALUES ("",
:fname, :lname,
:company, :telephone, :email, :adress, :zipcode,
:country, :note)');
$q->execute(array(":fname" => $_POST['voornaam'], ":lname" =>
$_POST['achternaam'], ":company" => $_POST['bedrijf'], ":telephone" =>
$_POST['telefoon'], ":email" => $_POST['email'], ":adress" =>
$_POST['adres'], ":zipcode" => $_POST['postcode'], ":country" =>
$_POST['land'], ":note" => $_POST['aantekening']));
I don't believe there's anything wrong there.
Here is my DB table:
Again, I believe it all matches up.
So, how is it possible that such a big error comes up for none of the default reasons?
Thanks.
You could try writing in the columns that you wish to insert to. Since your ID is AUTO INCREMENT, it might go wrong when you try to insert an empty string to that field.
$q = self::$connection->prepare('INSERT INTO users (firstname, lastname, company, phone, email, adress, zipcode, country, note) VALUES ("",
:fname, :lname,
:company, :telephone, :email, :adress, :zipcode,
:country, :note)');
$q->execute(array(":fname" => $_POST['voornaam'], ":lname" =>
$_POST['achternaam'], ":company" => $_POST['bedrijf'], ":telephone" =>
$_POST['telefoon'], ":email" => $_POST['email'], ":adress" =>
$_POST['adres'], ":zipcode" => $_POST['postcode'], ":country" =>
$_POST['land'], ":note" => $_POST['aantekening']));
I have a form which allows people to message each other, a user can select multiple people to message, when i submit a form it gives me the name and id of people selected to message in an array. uptil here i am able to get it to work for a single recipient
I want to be able to use this array and INSERT message for each user in different rows of mysql table
this is the array that i get when i submit a form
Array (
[to_user_id] => Array
(
[0] => 54
[1] => 55
)
[subject] => aaa
[message] => bbb
[send_message] =>
this is the part of code that works for a single recipient but not multiple
$to_user_id_array = ($_POST['to_user_id']);
$params = array(
':to_user_id' => $to_user_id_array,
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':sender_id' => $this->user_id,
':status' => "0",
':type' => "message",
':sender_name' => $sender_name,
':to_user_name' => $to_user_name,
':delete_received' => 'no',
':delete_sent' => 'no',
);
$sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
parent::query($sql, $params);
$this->error = "<div class='alert alert-success'>" . _('Your message has been sent.') . "</div>";
Will really appreciate any help..
This is what worked for me, i hope this helps someone else in similar position
while ($value = $stmt->fetch(PDO::FETCH_ASSOC)) {
$params = array(
':to_user_id' => $value['user_id'],
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':sender_id' => $this->user_id,
':status' => "0",
':type' => "message",
':sender_name' => $sender_name,
':to_user_name' => $value['name'],
':delete_received' => 'no',
':delete_sent' => 'no',
);
$sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
parent::query($sql, $params);
}
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']))