Discussion:
Insert Data Into Mysql
Robert
2014-01-10 21:53:36 UTC
Permalink
I have a date field on an html form that users may leave blank. If they do
leave it blank I want to write the date 01/01/1901 into the mysql table.
How can I accomplish this and where in my .php script file should I put the
code?

Thank You,
Robert
***@comcast.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ken Robinson
2014-01-10 22:19:18 UTC
Permalink
In your validation routine, check to see whether the date coming from
the user is blank or not (if it's not blank, make sure it's valid).

If it's valid, assign it to a variable in the format 'yyyy-mm-dd'. If
it's blank put '1901-01-01' in the variable.

When you do you MySQL statement use that variable. You should be
storing dates in the database as yyyy-mm-dd format, not mm/dd/yyyy.

Ken
Post by Robert
I have a date field on an html form that users may leave blank. If
they do leave it blank I want to write the date 01/01/1901 into the
mysql table. How can I accomplish this and where in my .php script
file should I put the code?
Thank You,
Robert
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-01-10 22:36:27 UTC
Permalink
Post by Robert
I have a date field on an html form that users may leave blank. If they
do leave it blank I want to write the date 01/01/1901 into the mysql
table. How can I accomplish this and where in my .php script file should
I put the code?
Thank You,
Robert
Why would you want to put any value in there? Leave it blank. Are you
editing the input to prevent someone from actually entering that date?

Where to put this in your php script? That's up to you certainly, but I
would make sure it was done before I posted my record.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jonesy
2014-01-11 02:48:23 UTC
Permalink
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If they
do leave it blank I want to write the date 01/01/1901 into the mysql
table. How can I accomplish this and where in my .php script file should
I put the code?
Thank You,
Robert
Why would you want to put any value in there? Leave it blank. Are you
editing the input to prevent someone from actually entering that date?
Where to put this in your php script? That's up to you certainly, but I
would make sure it was done before I posted my record.
It looks like a new semester has started....
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard L. Buskirk
2014-01-11 03:43:19 UTC
Permalink
Post by Jonesy
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If
they do leave it blank I want to write the date 01/01/1901 into the
mysql table. How can I accomplish this and where in my .php script
file should I put the code?
Thank You,
Robert
Why would you want to put any value in there? Leave it blank. Are
you
Post by Jim Giner
editing the input to prevent someone from actually entering that
date?
Post by Jim Giner
Where to put this in your php script? That's up to you certainly,
but
Post by Jim Giner
I would make sure it was done before I posted my record.
It looks like a new semester has started....
MySQL
You can set the value to NOW() or CURRENT_TIMESTAMP(), mysql functions and
format the date the way you want.

if(!empty$_POST['formdatefield'])) { // POST is just an example
$sql = "INSERT INTO yourtable SET datefield=DATE_FORMAT(NOW(),'%m\%d\%Y') ";
}



PHP
Validate the date has been set. If not set then set the value to system
current system date.

if(!empty($_POST['formdatefield'])) { // POST is just an example
$formdatefield = date("m/d/Y");
$sql = 'INSERT INTO yourtable SET datefield='.$formdatefield;
}

There are many way to validate POST or GET variables and declare a new
value.

Richard L. Buskirk
Oracle Certified MySQL 5.6 DBA (OCP)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
George Wilson
2014-01-11 18:42:20 UTC
Permalink
He wanted the default value to be 01-01-1901 though right?

I would do something like (assuming you are using a POST request and a form
field name 'indate'):

$inputDate = (($_POST['indate'])?convert_validate_date_function(
$_POST['indate']):"1901-01-01");

where convert_validate_date_function checks that the input is valid, if so
converts to a mysql friendly format and returns it, if not then handles it
however you want (IE return the default value, throw an exception, etc....).

Then plug that function into your prepared statement handle object and
execute the query. IE (Assuming you are using PDO and constructed your
connection object already):
try{
$sql = "INSERT INTO table_name (date_var) VALUES (?)";
$sth = $dbh->prepare($sql);
$sth->execute(array($inputDate));
}catch (PDOException $e){
logger->error("Exception caught: {$e->getMessage()}\n");

// other handling code such as die or redirect to error page....

}


On Fri, Jan 10, 2014 at 7:43 PM, Richard L. Buskirk <
Post by Richard L. Buskirk
Post by Jonesy
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If
they do leave it blank I want to write the date 01/01/1901 into the
mysql table. How can I accomplish this and where in my .php script
file should I put the code?
Thank You,
Robert
Why would you want to put any value in there? Leave it blank. Are
you
Post by Jim Giner
editing the input to prevent someone from actually entering that
date?
Post by Jim Giner
Where to put this in your php script? That's up to you certainly,
but
Post by Jim Giner
I would make sure it was done before I posted my record.
It looks like a new semester has started....
MySQL
You can set the value to NOW() or CURRENT_TIMESTAMP(), mysql functions and
format the date the way you want.
if(!empty$_POST['formdatefield'])) { // POST is just an example
$sql = "INSERT INTO yourtable SET datefield=DATE_FORMAT(NOW(),'%m\%d\%Y') ";
}
PHP
Validate the date has been set. If not set then set the value to system
current system date.
if(!empty($_POST['formdatefield'])) { // POST is just an example
$formdatefield = date("m/d/Y");
$sql = 'INSERT INTO yourtable SET datefield='.$formdatefield;
}
There are many way to validate POST or GET variables and declare a new
value.
Richard L. Buskirk
Oracle Certified MySQL 5.6 DBA (OCP)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
George Wilson
2014-01-11 19:34:53 UTC
Permalink
Ok, not to be anal retentive here but:
"$inputDate = (($_POST['indate'])?convert_validate_date_function(
$_POST['indate']):"1901-01-01");"
I am making as few assumptions as possible about your background so please
forgive me if I am preaching to the choir here but please do not hard code
that default value. Bad! Do this instead:

// In a convenient configuration section such as the top or in an included
script or something
$default_date = "1901-01-01";

$inputDate = (($_POST['indate'])?convert_validate_date_function(
$_POST['indate']):$default_date);

// As an after thought, instead of using a ternary operator like I have
here, you can just pass the POST argument into function which handles all
validation. I am not sure why I went with the Ternary operator. A full
function might be better.

function convert_validate_date_function($date_from_post, $default_date){
$out_date = $default_date;

if(isset($date_from_post)){
// check your validation criteria, day correct for month, month less than
12, year within bounds- all defined, etc... whatever you want
$out_date = <convert validated date to correct format>;
}

return $out_date;
}

This is one approach.

Oh well.
Post by George Wilson
He wanted the default value to be 01-01-1901 though right?
I would do something like (assuming you are using a POST request and a
$inputDate = (($_POST['indate'])?convert_validate_date_function(
$_POST['indate']):"1901-01-01");
where convert_validate_date_function checks that the input is valid, if
so converts to a mysql friendly format and returns it, if not then handles
it however you want (IE return the default value, throw an exception,
etc....).
Then plug that function into your prepared statement handle object and
execute the query. IE (Assuming you are using PDO and constructed your
try{
$sql = "INSERT INTO table_name (date_var) VALUES (?)";
$sth = $dbh->prepare($sql);
$sth->execute(array($inputDate));
}catch (PDOException $e){
logger->error("Exception caught: {$e->getMessage()}\n");
// other handling code such as die or redirect to error page....
}
On Fri, Jan 10, 2014 at 7:43 PM, Richard L. Buskirk <
Post by Richard L. Buskirk
Post by Jonesy
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If
they do leave it blank I want to write the date 01/01/1901 into the
mysql table. How can I accomplish this and where in my .php script
file should I put the code?
Thank You,
Robert
Why would you want to put any value in there? Leave it blank. Are
you
Post by Jim Giner
editing the input to prevent someone from actually entering that
date?
Post by Jim Giner
Where to put this in your php script? That's up to you certainly,
but
Post by Jim Giner
I would make sure it was done before I posted my record.
It looks like a new semester has started....
MySQL
You can set the value to NOW() or CURRENT_TIMESTAMP(), mysql functions and
format the date the way you want.
if(!empty$_POST['formdatefield'])) { // POST is just an example
$sql = "INSERT INTO yourtable SET datefield=DATE_FORMAT(NOW(),'%m\%d\%Y') ";
}
PHP
Validate the date has been set. If not set then set the value to system
current system date.
if(!empty($_POST['formdatefield'])) { // POST is just an example
$formdatefield = date("m/d/Y");
$sql = 'INSERT INTO yourtable SET datefield='.$formdatefield;
}
There are many way to validate POST or GET variables and declare a new
value.
Richard L. Buskirk
Oracle Certified MySQL 5.6 DBA (OCP)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jasper Kips
2014-01-11 22:59:36 UTC
Permalink
Sincerely,

Jasper Kips
Post by Richard L. Buskirk
Post by Jonesy
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If
they do leave it blank I want to write the date 01/01/1901 into the
mysql table. How can I accomplish this and where in my .php script
file should I put the code?
Thank You,
Robert
Why would you want to put any value in there? Leave it blank. Are
you
Post by Jim Giner
editing the input to prevent someone from actually entering that
date?
Post by Jim Giner
Where to put this in your php script? That's up to you certainly,
but
Post by Jim Giner
I would make sure it was done before I posted my record.
It looks like a new semester has started....
MySQL
You can set the value to NOW() or CURRENT_TIMESTAMP(), mysql functions and
format the date the way you want.
if(!empty$_POST['formdatefield'])) { // POST is just an example
$sql = "INSERT INTO yourtable SET datefield=DATE_FORMAT(NOW(),'%m\%d\%Y') ";
}
PHP
Validate the date has been set. If not set then set the value to system
current system date.
if(!empty($_POST['formdatefield'])) { // POST is just an example
$formdatefield = date("m/d/Y");
$sql = 'INSERT INTO yourtable SET datefield='.$formdatefield;
}
There are many way to validate POST or GET variables and declare a new
value.
Richard L. Buskirk
Oracle Certified MySQL 5.6 DBA (OCP)
And of course, it is almost mandatory to use prepared statements. Especially if the source of the data is the internet.
Post by Richard L. Buskirk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
George Wilson
2014-01-13 13:26:16 UTC
Permalink
Post by Jasper Kips
Sincerely,
Jasper Kips
....
And of course, it is almost mandatory to use prepared statements.
Especially if the source of the data is the internet.
Here here!
So, I do not mean to get on a soap box and shout the glories of a preferred
library (ok, yeah I guess I do- why lie). Robert, you did not mention what
your skill level is but my best advice regarding php/mysql development,
besides getting into the habit of always using prepared statements, is to
learn php's PDO library. People tend to have their favorites (for instance-
my current company uses mysqli. In fact, the previous lead developer
before me had a policy of only using mysqli). PDO has a very clean prepared
statement system including named parameters which not only guards against
potential sql-injections (please google search it if you are not familiar
with the concept- very important to know), it also makes constructing
complex but parameterizable queries much much easier. If you ever do
development against a normalized data warehouse for instance, you will
likely have to do queries involving a bunch of joins, unions, etc... You
can still accomplish that with placeholders (for instance, my example in a
previous email) but you have to keep track of argument ordering and such.
Prepared statements also increase performance since they are compiled on
the sql server side. If you have to execute the same query a bunch of
times, you will noticed a big performance increase (I have stories I can
tell about some perl scripts I wrote a few years ago). As a nice bonus, PDO
supports 12 databases including mysql and Oracle, vs mysqli which to my
knowledge only supports mysql. PDO is the first php/DB library I learned
and I am very glad I did.

Just a thought- bouncing off of Jasper's comment.

Jim Giner
2014-01-11 19:09:43 UTC
Permalink
Post by Robert
I have a date field on an html form that users may leave blank. If they
do leave it blank I want to write the date 01/01/1901 into the mysql
table. How can I accomplish this and where in my .php script file should
I put the code?
Thank You,
Robert
Robert,
It really makes no sense to post a completely fabricated value into your
date field to represent 'the absence' of a date. From now on you will
be forced to mask that value OUT of your output displays since I'm
fairly certain you will not want to show people that silly date value.
Do you realize that just leaving the date = null you will accomplish the
same thing and you won't have to 'hide' it when you call up a record and
send it to the client?

Can you give us one valid reason that you wish to do this thing?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
George Wilson
2014-01-11 19:46:38 UTC
Permalink
Hi Jim,
With all due respect, I think you have made some good arguments and I would
tend to agree with you. All things considered, usually using a default
value like that will be a liability later. However, we dont know what this
guy is up to. There could be good reason for using that default date.
Perhaps he or his boss wants a value there because of database or indexing
constraints and they do not have control of the schema- who knows. The guy
asked his question with the particular conditions he is interested in.
Perhaps your arguments will sway him but is there really a need to grill
the guy?
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If they
do leave it blank I want to write the date 01/01/1901 into the mysql
table. How can I accomplish this and where in my .php script file should
I put the code?
Thank You,
Robert
Robert,
It really makes no sense to post a completely fabricated value into your
date field to represent 'the absence' of a date. From now on you will be
forced to mask that value OUT of your output displays since I'm fairly
certain you will not want to show people that silly date value. Do you
realize that just leaving the date = null you will accomplish the same
thing and you won't have to 'hide' it when you call up a record and send it
to the client?
Can you give us one valid reason that you wish to do this thing?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-01-11 22:17:33 UTC
Permalink
Post by George Wilson
Hi Jim,
With all due respect, I think you have made some good arguments and I would
tend to agree with you. All things considered, usually using a default
value like that will be a liability later. However, we dont know what this
guy is up to. There could be good reason for using that default date.
Perhaps he or his boss wants a value there because of database or indexing
constraints and they do not have control of the schema- who knows. The guy
asked his question with the particular conditions he is interested in.
Perhaps your arguments will sway him but is there really a need to grill
the guy?
Post by Jim Giner
Post by Robert
I have a date field on an html form that users may leave blank. If they
do leave it blank I want to write the date 01/01/1901 into the mysql
table. How can I accomplish this and where in my .php script file should
I put the code?
Thank You,
Robert
Robert,
It really makes no sense to post a completely fabricated value into your
date field to represent 'the absence' of a date. From now on you will be
forced to mask that value OUT of your output displays since I'm fairly
certain you will not want to show people that silly date value. Do you
realize that just leaving the date = null you will accomplish the same
thing and you won't have to 'hide' it when you call up a record and send it
to the client?
Can you give us one valid reason that you wish to do this thing?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
I can only attribute his choice of this behavior to inexperience and am
trying to give him something to think about before proceeding.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Meyer
2014-01-11 22:24:17 UTC
Permalink
While that may be true, there probably is a slightly better way to word
that ("is there a reason why you inserting a dummy date rather than
leaving it null and letting your application mask it later?" vs. "can
you give us one valid reason?"). We're supposed to be here to help, and
giving a new user the third degree is hardly helpful.
Post by Jim Giner
I can only attribute his choice of this behavior to inexperience and
am trying to give him something to think about before proceeding.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...