I am in problem is that, i am using php ms access databse, when i am trying to update date in database its not updating, i want to update date field when user put date and when date is null column value should be null, my code following
if($cDate==NULL){
$cDate='Null';
}
else {
$cDate='02/03/2016';
}
if($buttonName=="Update"){
$sql_update="UPDATE 0D1_INDEX set C_date=$cDate where DN_='$mdn'";
and when date inserted it become time
Try This,
Set Default Value to NULL in Database
If I understand your question correctly you could use the isset() function to determine if the value of $cDate is null.
So you would have to do something like so:
if(!isset($cDate)){
$cDate = "Null";
} else{
$cDate='02/03/2016';
}
isset() determines if the value is not NULL so putting an ! in front of it means that if the value is NULL
Related
I am creating a form that extracts user info, and the users can update their info if needs be. On the form there are two date fields, the second field is optional. So when the user clicks UPDATE and the second date field is blank, in MSSQL database it updates the date_two field with "01/01/1970". I am using PHP with MSSQL, so in the code I have:
if(isset($_POST['update']) {
$date_two = $_POST['date_two'];
if($date_two == "")
$date_two == NULL;
else
$date_two = date("Y-m-d", strtotime($date_two) );
UPDATE table SET date_two = CAST('$date_two' as DATE2);
}
The resulted entry is 01/01/1970, instead of NULL.
you should check if your input date value is a emprty string ('') and manage for null because if the value of your var is not null (also an empty string)
the update produce a date value
UPDATE table SET date_two = (case when '$date_two' = '' then NULL
else CAST('$date_two' as DATE2)
end);
anyway you should not use php var in your sql command you are at rusk for sqlinjection .. take a lool at you db driver fior prepared command and binding param ..
I am facing one issue. In my MySQL datetime field some value is storing like 0000-00-00 and I need to check these value. I am explaining my code below.
if (strtotime($data['actualdateofaudit']) == 0) {
$data['actualdateofaudit']='';
}else{
$data['actualdateofaudit'] = date('d-m-Y', strtotime($data['actualdateofaudit']));
}
Here actualdateofaudit is my datetime field column name. In my case I could not check If there is any value like 0000-00-00 this. Here I need to check these type value.
Try This
if (strtotime($data['actualdateofaudit'])) {
$data['actualdateofaudit'] = date('d-m-Y', strtotime($data['actualdateofaudit']));
}else{
$data['actualdateofaudit']='';
}
Ok, this is driving me crazy!!
Got a MySQL table with an integer field default set to null.
Need to update it through a form submission. If form field is blank, set database field to NULL, ELSE set database field to form submission value.
So, in PHP I have: (staging code, still needs to be sanitized)
if($_POST['vendor-product'] != ''){
$ms2VendorID = $_POST['vendor-product'];
}else{
$ms2VendorID = '';
}
The problem is that instead of NULL it enters a zero. I've tried several different ways and nothing seems to be working.
Thanks for any help.
Why don't you assign NULL (as a string) to your variable in the else statement?
if($_POST['vendor-product'] != ''){
$ms2VendorID = $_POST['vendor-product'];
}else{
$ms2VendorID = "NULL";
}
Also, make sure that the field has not the NOT NULL constraint enabled.
$record['field'] = null;
if (isset($record['field']) || is_null($record['field'])) {
// When $record['field'] is set and not null, OR, when $record['field'] = null
// Basically, when $record['field'] is defined as $record['field'] = whatever, even null.
} else {
// When $record['field'] is not defined at all, not even null
}
My intention is to check if $record['field'] is defined at all, even null.
However every time $record['field'] is not set at all (e.g. comment out $record['field'] = null;), it gives me this error:
Undefined index: field
In case anyone would ask, this is for when NULL values become meaningful, such as in a SQL query assigning a NULL value to a timestamp column would make MySQL update it with the current timestamp.
Any way I can do this?
Since what you have here is not just a variable but an associative array, you can use array_key_exists() to check that the array contains a field with that name, regardless of its value.
I have some records stored through Parse PHP SDK.
Among the fields there is a Date field which is initialized with an undefined value.
On the go this value may change either to a real date or a null value, in case I set a date and then want to "unset" it. What I am doing is setting it as null.
The problem is that when I want to filter the set dates and all the others (in my case both undefined and null values should be the same, i.e. "unset") I try a query filter similar to the following:
$query->equalTo("mydatefield", NULL);
The weird thing about it is that it only returns the undefined values, not the NULL ones.
On the other hand when I use
$query->notEqualTo("mydatefield", NULL);
the desired records are all returned just fine.
Any idea how to accomplish getting both null and undefined values?
Thanks in advance.
You are correct, it doesn't seem to work with the PHP library. The same thing works fine with the Javascript library.
You can accomplish what you want though by excluding the values that are not null (or undefined). Here is an example:
use Parse\ParseQuery;
//fetch objectIds of all data that is not null
$query = new ParseQuery("tabledata");
$query = $query -> notEqualTo("mydatefield", null);
$results = $query -> find();
$keys = array();
for ($i=0; $i<count($results); $i++){
array_push($keys, $results[$i]-> getObjectId());
}
//now fetch all records but the records with the null column
$query2 = new ParseQuery("tabledata");
$query2 = $query2 -> notContainedIn("objectId", $keys);
$results2 = $query2 -> find();
You can filter queries with null fields with exists and doesNotExist methods of ParseQuery class.
$query->exists("mydatefield"); // returns rows that do not have null value for mydatefield key.
$query->doesNotExist("mydatefield"); // returns rows that have null value for mydatefield key.