I want to send a value of "NULL" to a column in mySQL.
$ParentEventID = "NULL";
mysql_query("UPDATE events SET
ParentEventID = '$ParentEventID'");
The column keeps defaulting to "0". I have the column set to accept NULL and I can edit the cell with the NULL check box.
I need to not set the default to NULL because it may effect code in other places.
You are on the right track making "NULL" a string. If you need to set it NULL, then remove the qoutes around $ParentEventID.
mysql_query("UPDATE events SET ParentEventID = $ParentEventID");
However, before doing so make sure that the value of $ParentEventID === NULL.
Related
I have the following which only inserts to column two if the value is empty:
UPDATE name
SET one=?,
two=COALESCE(two, ?),
three=?
WHERE id = ?
However, this only works if column two if the value is null. How can I get this to work if it is either NULL OR an empty string.
It should always insert to one and three. It should only insert to two if it is NULL or empty
I am using SQLite with PHP
If by "empty" you mean an empty string, you can use nullif():
two = COALESCE(NULLIF(two, ''), ?)
The more general solution is a CASE expression.
Don't store empty strings in TEXT columns.
Update the table to replace all empty strings with NULLs:
UPDATE name
SET two = NULL
WHERE TRIM(two) = ''
Now you can use your query, which is correct and in any other query you only need to check for NULL.
Need to update a field in database, but when I make a request, it writes me "0 rows affected" although the image field is, and it is empty.
UPDATE `oc_product` SET `image`= 'no_image.png' WHERE `image`='';
Your query is checking for a value of an empty string, although from what you have shown the value you are actually looking to change has a value of NULL. These are two different values, NULL isn't equal to ''. As such if you are looking to replace items that have a NULL value you need to change your where statement for that (WHERE image IS NULL):
UPDATE `oc_product` SET `image`= 'no_image.png' WHERE `image` IS NULL;
Did select query returns any rows
select image from oc_product WHERE image='';
If so add other field confition as well to check whether it is getting affected ot not example
UPDATE oc_product SET image= 'no_image.png' WHERE (image= "" AND image IS NULL) and other_column ='column value';
Hope this helpful for you :)
I'm sure I am missing something quite simple, but I have a form with a few yes/no inputs - type=radio. I have made the default NULL. YES is significant, NO is significant and NULL means user has never answered the question. When I UPDATE the db with user's selections, my NULL values are overwritten with 0 even though NO is not selected. I've read that if user doesn't choose either YES or NO that variable is NOT SET, therefore,
$licensedYN = (isset($_POST['licensed'])&&!empty($_POST['licensed']))?$_POST['licensed']:NULL;
$malpracticeYN = (isset($_POST['malpractice']))?$_POST['malpractice']:NULL;
$sqlAdd = $db->query("INSERT INTO temp (licensedYN,malpracticeYN) VALUES ('$licensedYN','$malpracticeYN')");
How do I get NULL to be inserted into DB and not '0'?
To allow NULL as a value in your licensedYN column... Run this query..
ALTER TABLE temp MODIFY licensedYN varchar(255) null;
This would allow you store null values in your db.
In your case you can just remove the quotes from the query and place it in the value as follows:
$licensedYN = (isset($_POST['licensed']) && !empty($_POST['licensed']))? "'{$_POST['licensed']}'" : 'NULL';
$malpracticeYN = (isset($_POST['malpractice'])) ? "'{$_POST['malpractice']}'": 'NULL';
$sqlAdd = $db->query("INSERT INTO temp (licensedYN,malpracticeYN) VALUES ($licensedYN,$malpracticeYN)");
In any case, you should consider working with prepared statements instead of concatenating strings. You will prevent many problems as sql injections, boost the query execution time and will not have headaches with quotation anymore...
I want not update a record when the variable is empty when executing a UPDATE query in SQL. However, when the variable is filled, the record should be updated.
So in example, the myAge field in the database have currently a value of 20 (type int).
After executing the following query, the record should still be 20.
$age = '';
db_con->query("UPDATE info SET myAge = ".$age." WHERE account_id = 1");
Ps: I know I could check if the variable is empty with PHP, but I was wondering If this could be archieved within SQL?
You could use an IF construct in SQL to check if the value is empty:
$age = mysql_real_escape_string($age);
db_con->query("UPDATE info SET myAge = IF('".$age."' = '', myAge, ".$age.") WHERE account_id = 1");
If the passed PHP variable is empty, you set the old myAge.
Checking in PHP makes more sense, you might save a database query.
The way you are passing that variable to your database is potentially dangerous. If you didn't know about mysql_real_escape_string, look it up NOW.
Better yet, start using a database wrapper that escapes values for you.
I would add the condition into the where, if you only care about one field.
It is not clear what you mean by empty. That could be either NULL or a blank string. My guess is that myAge is a number, so NULL would be "empty":
UPDATE info
SET myAge = ".$age."
WHERE account_id = 1 AND myAge IS NOT NULL;
You can also do this in the SET, if you like:
UPDATE info
SET myAge = (CASE WHEN myAge IS NOT NULL THEN ".$age." END)
WHERE account_id = 1;
This is necessary if you have multiple columns that you want to update like this. I much prefer CASE over IF(), because CASE is ANSI standard and available in most databases.
I have a if NULL echo "Message" else $value.
There is some JavaScript hiding the input field, so they check it and enter the value.
If they don't check it to enter the field, the db enters 0.00 and doesn't specify NULL so my PHP if statement works.
How do I either set the variable as NULL if its blank, or set NULL in the INSERT statement?
`myTableField` decimal(10,2) default NULL,
Not sure exactly what you're after, but you can run the below if statement to check what value was entered, and set to null accordingly:
if(!is_numeric($fieldvalue) || $fieldvalue==0){
// if the entered value isnt a number (i.e. isnt entered, or invalid) or if the value is zero, sounded like it was your default
$fieldvalue=NULL;
// could also use unset($fieldvalue);
}
If the variable $fieldvalue is set to null (or un-set), it will be inserted as a NULL in your DB according to your field definitions. Make sure your insert statement references the value without ' or " encapsulating figures however (not needed as its a decimal field).