postgres enum insertion using pg_query - php

I have a table with one field of enum type. I execute the following query using pg_query
INSERT INTO users (email, facebook_id, first_name, middle_name, last_name, birth_date, password, gender, school_id, timezone, email_verified, role_id) VALUES ('robert#1599309412.com', NULL, 'Robert', '', 'George', '1984-05-20', 'Some password', 'MALE', 1, '0.0', false, 1 );
pg_query($connection, 'INSERT INTO users (email, facebook_id, first_name, middle_name, last_name, birth_date, password, gender, school_id, timezone, email_verified, role_id) VALUES (\'robert#2084537193.com\', NULL, \'Robert\', \'\', \'George\', \'1984-05-20\', \'Some password\', \'MALE\', 1, \'0.0\', false, 1 )')
but i get error as below.
PHP Warning: pg_query(): Query failed: ERROR: invalid input value for enum sex: "MALE"
LINE 1: ...rt', '', 'George', '1984-05-20', 'Some password', 'MALE', 1,...
Direct execution of query in postgres client don't give this error.What is solution for this?

Please make sure cases match. Postgres is not too strict about it (consider the table names), but it might worth a check.
Having a gender attribute enum'd is unnecessary overhead in my opinion, you're better off having a male int column with 1, 0 values and maybe null for unknown/not specified. With values greater than 1 it's still future-proof just in case a new gender appears. :)
If you really are attached to enums, you might want to consider using PDO, because it makes binding values a lot easier and a lot more secure.

Related

when i insert data its showing database error

When i was going to insert a student in my web app it was showing these error can anyone help me out.
Error Number: 1364
Field 'user_status' doesn't have a default value
INSERT INTO users (first_name, last_name, phone, profile_image, username, password, email, ip_address, created_on, last_login, active) VALUES ('dinesh', 'kumar', '+919591812528', 'fa07b9fc131dc4c7caadac44539c81c4.JPG', 'dinesh kumar', '$2y$08$8KVwr6dQWp8GtsXcFPrRsujqFIBGwdu9MEgMniAXdznXQifCHuDe2', 'dineshprivacy007#gmail.com', '49.207.177.226', 1503992683, 1503992683, 1)
Filename: /home/aspireng/aspiresms/models/ion_auth_model.php
Line Number: 872
user_status Column doesnt have a default value set, kindly set this field NULL as default, and then run query, or insert some value to this field.
if user_status field in your table, please set default value NULL if not assign.
This means there is a user_status column in you db which is expecting some value to be passed in. if you dont have any value to pass then do something like this
INSERT INTO users (first_name, last_name, phone, profile_image, username, password, email, ip_address, created_on, last_login, active,user_status) VALUES ('dinesh', 'kumar', '+919591812528', 'fa07b9fc131dc4c7caadac44539c81c4.JPG', 'dinesh kumar', '$2y$08$8KVwr6dQWp8GtsXcFPrRsujqFIBGwdu9MEgMniAXdznXQifCHuDe2', 'dineshprivacy007#gmail.com', '49.207.177.226', 1503992683, 1503992683, 1,'0')
your field user_status cannot be null so you need to pass any value in it. I just passed 0 for reference.

PHP/MySQL Using Variables and INSERT INTO syntax

I am trying to pass variable values to a MySQL database table. I am using a PDO to get access to the database, and am able to echo the variable values that I want to insert to my browser. The only thing I can think of is that my syntax is wrong. I am clearly a novice at using PHP/MySQL.
I am not getting any errors. The info isn't going into my table. What am I doing wrong?
$sql = "INSERT INTO testquiz (version, points, passing_percent, gained_score, username, email, quiz_title, date)
VALUES ('$version', $points, $passing_percent, $gained_score, '$username', '$email', '$quiz_title', CURDATE() )";
Query to create table:
MySQL CREATE TABLE Query:
CREATE TABLE testquiz (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
version TEXT,
points INT,
passing_percent DOUBLE,
gained_score DOUBLE,
username TEXT,
email TEXT,
quiz_title TEXT,
date DATE NOT NULL
) DEFAULTCHARACTER SET utf8 ENGINE=InnoDB
When using PDO, the generally accepted practice is to use prepared statements for SQL, which essentially are a method used to sanitize your string input.
If your database connection object is $dbo then it would usually go like this.
Create a prepared statement by calling the prepare method on your database connection object:
$sql = $dbo->prepare("INSERT INTO testquiz (version, points, passing_percent, gained_score, username, email, quiz_title, date)
VALUES (:version, :points, :passing_percent, :gained_score, :username, :email, :quiz_title, CURDATE())");
As you can see, instead of passing in the variables I want for the values directly, I've created placeholders. Then, call the execute method on the $sql obect and pass the values in for the placeholders as key-value pairs in an array.
$sql->execute(array(":version" => $version, ":points" => $points, ":passing_percent" => $passing_percent, ":gained_score" => $gained_score, ":username" => $username, ":email" => $email, ":quiz_title" => $quiz_title));
This code passes in the values you define instead of the placeholders, and it properly escapes and sanitizes the variables you pass in for security, while executing your INSERT statement.
http://us1.php.net/pdo.prepared-statements
Change the insert statement to the below format and try.
$sql = "INSERT INTO testquiz (version, points, passing_percent, gained_score, username, email, quiz_title, date)
VALUES ('".$version."', '".$points."', '".$passing_percent."', '".$gained_score."', '".$username."', '".$email."', '".$quiz_title."', CURDATE())";

Basic insert into SQL

I know this is really simple i haven't touched PHP and SQL in a few years.
This works
$mysqli->query("INSERT INTO sonyCES2013.registration (id,
firstName, lastName, eMail, telephone, outlet, comfirm,
preferTime, date) VALUES (NULL,Cat, 'Catherine',
'Cat#gmail.com', '123-456-4561', 'Some Text', 'Yes', '4:00pm'
,'1/09/14')");
this doesn't work
$mysqli->query("INSERT INTO sonyCES2013.registration (id,
firstName, lastName, eMail, telephone, outlet, comfirm,
preferTime, date) VALUES
(NULL,{$fName},{$lName},{$eMail},{$telephone},{$outlet},{$comfirmation},{
$preferTime},{$day})");
help and yes i did check that the variables aren't empty and i did try without the `` between each {}
You need to add ' ' to the variables to make the interpeter able to understand what you are trying to do ( in this case passing some php variable as parameters)
Use this and see if it works:
$mysqli->query("INSERT INTO sonyCES2013.registration (id, firstName, lastName,
eMail, telephone, outlet, comfirm, preferTime, date) VALUES
(NULL,'$fName','$lName','$eMail','$telephone','$outlet',
'$comfirmation','$preferTime','$day')");
It won't work that way because the resulting SQL looks something like this:
$mysqli->query("INSERT INTO sonyCES2013.registration (id, firstName, lastName, eMail, telephone, outlet, comfirm, preferTime, date) VALUES (NULL,Cat, Catherine, Cat#gmail.com, 123-456-4561, Some Text, Yes, 4:00pm ,1/09/14)");
Note the lack of single quotes around the strings, which make the SQL invalid.

How to handle nulls in this PHP/mysql insert statement

I sometimes have arrays with null or empty values. In MySql they are set up to be non-nullable, but they have default values assigned. Why then, does MySql give me an error, e.g. Column user_type can not be null. (I'm not running in strict mode).
I realise that I could use the keyword DEFAULT in place of some values when preparing the query, but I don't really want to do that. I would like to be able to write my SQL statements verbatim, rather than put them together with foreach loops, etc. For example, I would like to use "INSERT INTO users (user_type, first_name, last_name, password) VALUES (:user_type, :first_name, :last_name, :password)";
As far as I recollect, this was working fine (i.e. substituting in the correct defaults) until I moved from using ? query markers to named parameters...
Thanks...
I would create a function that accepts the values as parameters. It would have the default values in an associative array. If the value for any of the parameters is null, it would replace it with the default.
eg
function setUpQuery($user_type_in, $first_name_in, $last_name_in, $password_in){
$default_values('user_type' => 'Admin', 'first_name' => 'John', 'last_name' => 'Doe', 'password' => 'XXX');
$user_type = ($user_type_in == NULL)? $default_values['user_type']:$user_type_in;
.....
return "INSERT INTO users (user_type, first_name, last_name, password) VALUES ('$user_type', '$first_name', '$last_name', '$password');"
}
Good Point. How about the following:
INSERT INTO users(user_type, first_name, last_name,password) values
(ifnull('$user_type',default(user_type)), ifnull('$first_name', default(first_name)),
ifnull('$last_name',default(last_name)), ifnull('$password', default(password));

MySQL error when submitting '' in an array

I have a PHP loop that's auto-generating an array to INSERT into a table. But I get this error:
Database query failed: Incorrect integer value: '' for column 'id' at row 1
Last SQL query: INSERT INTO users(id, email, password, first_name, last_name) VALUES ('', 'test#user.com', 'fljhdsfsd', 'John', 'Doe')
I've tried setting the id field as No Null and Null, but that didn't help.
Any ideas? Thanks in advance for your help.
If your id field is an auto-incremented column then you can simply omit it from the INSERT query.
INSERT INTO users(email, password, first_name, last_name)
VALUES ('test#user.com', 'fljhdsfsd', 'John', 'Doe');
An id will be generated automatically.
Your Mysql is running in the strict mode. You have to pass the NULL if you have no value for that column, if the column is auto increment you can keep out this column from query.
Once try the below query, I believe this will work.
INSERT INTO users(id, email, password, first_name, last_name)
VALUES (NULL, 'test#user.com', 'fljhdsfsd', 'John', 'Doe')
If id field is auto incremented you can skip that field. If not then read error, it says "Incorrect integer value" and you are giving '' witch is string. In this case before every insert you should fetch max id from database and increment it, or use any other ID generation.
This is happened because your id field in database now contains only integer value. and blanck is not a integer value. Just change the data type on your database.
Just change your datatype id "integer" to "varchar", tick out the "auto increment" and put a length like "250" field in database, then run your query.

Categories