how to input single data in insert query - php

The error is:-
Field 'attn' doesn't have a default value41
If I set all the value of column query run perfectly like :
$stuattend = "INSERT INTO tblattendance(roll, attandence, att_time) VALUES('$roll', 'present', '5:30')";
I guess my problem is when I execute one value (roll) through query.
What would be the solution?

Hi the error clearly states that you have a column named as attn which is not nullable field by default. Either provide the default value or make it nullable or pass it some value so that it stops throwing the error.
You can change the above query to assign the default value as follows :
NOTE : Here you may assign the any value to it. Thinking that it may be integer I am doing as follows
INSERT INTO tblattendance(roll, attandence, att_time, attn) VALUES('$roll', 'present', '5:30', 10)

Related

Value not compatible when doing merge in DB2

I'm trying to perform a merge based on a parameter from a previous select within a php script but I"m getting the error "SQL0408 - Value for column,variable, or paramter QUANTITY not compatible"
In my destination table QUANTITY is data type INTEGER
In my select query, I'm casting the value as an int (which it already is in the table, I'm just casting everything to be safe)
cast(MAX(orqtyc) as int) AS QUANTITY,
Then in my MERGE I'm casting as INT
MERGE INTO HNORMANTEST.PLACEMENTS AS P
USING(VALUES(
CAST(:QUANTITY as INT),
))
Using this param
$params = [
":QUANTITY" => $row["QUANTITY"],
];
Why would it say it's not compatible?
Did you try it by putting directly value instead of via param and see if it works or not.
Another thing you can try is remove the first casting which may not be needed. As you said QUANTITY is already a INT datatype.
Please try both the variation. If both variation givens same error then there might be some product limitation/bug.
You need to pass on the DB2 version where you have tried to look further in it.

How to pass default value for relation fields while inserting a record through doctrine in symfony2?

How to pass default value for relation fields while inserting a record through doctrine in symfony2?
My entity insert query is as follows
$this->hospitalAdminAppointment->setAppDetail($this->venReqAppReason)
->setvendor($vendor)
->setHospAdminApp($HospitalRequestedAppointment)
->setAppSuperAdmin($superAdmin)
->setPostedByHospSuperUser($postedByHospSuperUser)
->setAppPrimaryCompany($primaryCompany)
->setAppSecondaryCompany($secondryCompany)
->setAppDepartment($this->deptName)
->setAppDate(new \DateTime($this->appointmentDate))
->setAppTime(new \DateTime($this->appointmentTime))
->setAppPerson($person)
->setAppAdddedBy(AppointmentRequestAddedByType::VENDOR_REQUEST)
->setAppFromCalender('')
->setVendorRequestedAppointment($vendorRequestedAppointment)
->setAppHospital($hospital);
$this->em->persist($this->hospitalAdminAppointment);
$this->em->flush();
Here "AppSuperAdmin" field is in relation to another table and its type is integer and "not allow null" it's taking "0" through normal MySQL query.
We can't get AppSuperAdmin value for every time. Sometimes we need to pass it just zero.
Please suggest.
It all depends on how you are setting the value for variable $superAdmin. The easiest way is to check if $superAdmin is set, if yes set that value, otherwise set 0 like this:
->setAppSuperAdmin($superAdmin? $superAdmin : 0)

postgresql insert null value on query

In my php code query, i got some error when i will place a null value
into the table. The column names "number1","number2", and "number3" are integers and can have a null value.
if there is no null value, the query works
query is like this
insert into table(number1,number2,number3) values (1,2,3);
but when I leave a blank value on the input form on the PHP,
for example I will not place value for column "number2", the query will look
like this.
insert into table(number1,number2,number3) values (1,,3);
it says ERROR: syntax error at or near ","
SQL state: 42601
Does anybody have an idea how to solve this?
this query is likely to be the solution but is there any other way?
insert into table(number1,number2,number3) values (1,null,3);
because i got so many variables like that and am a bit lazy to place some conditions like when that variable returns a blank value then value="null"
You insert NULL value by typing NULL:
INSERT INTO table(number1,number2,number3) VALUES (1,NULL,3);
If you have a variable and when that variable is empty you want to insert a NULL value you can use NULLIF with the variable enclosed in single quotes to prepare for that (this is somewhat dirty solution as you have to treat the variable as an empty string and then convert it to integer):
INSERT INTO table(number1,number2,number3) VALUES (1,NULLIF('$var','')::integer,3);

Bind not required (empty) parameter into a PDO query

I have created a "Feedback" section on a website. In this section, I've included a text field for comments and star rating system. The rating input is not required (people are not forced to leave rates) and is used to create a $_POST['rating'] integer variable (1-5) which is binded into an insert PDO query:
$dbms->bindparam(':rating', $_POST['rating'], PDO::PARAM_INT);
This works well when $_POST['rating'] variable is set, but when it's not I get the following error:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'rating' at row 1
Is there any way (i.e. a flag) to tell PDO to bind a parameter only if it's not empty and use MySQL default value instead?
if(empty($_POST['rating'])) $_POST['rating'] = 0;
Somewhere before your bind. This will thus always give PDO an int value regardless of user action. If rating isn't set, the rating becomes 0.

Why an ENUM("0", "1") is saved as an empty string in Mysql?

I have a MYSQL table with an ENUM field named "offset" and some other columns. The field is defined as:
ENUM(0,1), can be NULL, predefined value NULL
Now I have two server. A production server and a development server and the same PHP script used to create and to update the database.
First step: the application create the record witout passing the "offset" in the CREATE query.
Second step: the application ask to the user some data (not the "offset" value), read the row inserted in step one and make an array, update some field (not the "offset" field), create a query in an automated fashion and save the row again with the updated values.
The automated query builder simple read all the field passed in an array and create the UPDATE string.
In both systems I obtain this array:
$values = array(... 'offset' => null);
and convert it in this same query passing the values in the mysql_real_escape_string:
UPDATE MyTable SET values..., `offset` = '' WHERE id = '10';
Now there is the problem. When i launch the query in the production system, the row is saved, in the development system I got an error and the db says that the offset data is wrong without saving the row.
From phpmyadmin when I create the row with the first step, it shows NULL in the offset field. After saving the field in the system which give no errors, it show me an empty string.
Both system are using MySQL 5 but the production uses 5.0.51 on Linux and development use 5.0.37 on Windows.
The questions:
Why one system give me an error an the other one save the field ? Is a configuration difference ?
Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?
Why one system give me an error an the other one save the field ? Is a configuration difference ?
Probably. See below.
Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?
According to the MySQL ENUM documentation:
The value may also be the empty string ('') or NULL under certain circumstances:
If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a "normal" empty string by the fact that this string has the numeric value 0. ...
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
(Emphasis added.)
strager's answer seems like a good explanation on why your code behaves differently on the 2 environments.
The problem lies elsewhere though. If you want to set a value to NULL in the query you shound use exactly NULL, but you are using mysql_real_escape_string() which result is always a string:
$ php -r 'var_dump(mysql_real_escape_string(null));'
string(0) ""
You should handle this differently. E.g:
$value = null
$escaped_value = is_null($value) ? "NULL" : mysql_real_escape_string($value);
var_dump($escaped_value);
// NULL
Some DB layers, like PDO, handle this just fine for you.
If you want it to be NULL, why don't you do this in the first place:
UPDATE MyTable SET values..., `offset` = NULL WHERE id = 10;

Categories