Re: Re: Insert Data Into Mysql
| From: | George Wilson | Date: | Sat, 11 Jan 2014 19:34:53 +0000 |
| Subject: | Re: Re: Insert Data Into Mysql | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322675@lists.php.net to get a copy of this message | ||
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.
On Sat, Jan 11, 2014 at 10:42 AM, George Wilson <rmwscbt@gmail.com> wrote:
> 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 <
> admin@buskirkgraphics.com> wrote:
>
>> > On Fri, 10 Jan 2014 17:36:27 -0500, Jim Giner wrote:
>> > > On 1/10/2014 4:53 PM, Robert wrote:
>> > >>
>> > >> 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
>> > >> flahurr@comcast.net
>> > > 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....
>> >
>>
>>
>> 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
>>
>>
>