Page 1 of 1

PDO and MySQL date field

Posted: Thu Apr 15, 2010 3:59 pm
by proveyourselfthom
I have the following table:

Code: Select all

CREATE TABLE IF NOT EXISTS `projetos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `descricao` text NOT NULL,
  `data_recebimento` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
And the following PHP code (insert method):
project.php

Code: Select all

<?php
class Project {
        private $description;
        private $files;
        private $receiveDate;

        public function __construct($description, $files = null, $receiveDate = null) {
                $this->description = $description;

                if(is_array($files)) {
                        $this->files = $files;
                }
                else {
                        $this->files = array();
                        $this->addFile($file);
                }

                $this->receiveDate = $receiveDate;
        }

        public function addFile(File $file) {
                array_push($this->files, $file);
        }

        public function insert() {
                $database = Connection::getConnection();

                $statement = $database->prepare("INSERT INTO projetos (descricao, data_recebimento) VALUES ('?', '?')");

                $statement->bindParam(1, $this->description);
                $statement->bindParam(2, $this->receiveDate);

                $statement->execute();
        }
}
?>
form:

Code: Select all

<?php
require_once('../classes/connection.php');
require_once('../classes/project.php');

// Next month = days multiplied by hours multiplied by minutes multiplied by seconds...
$nextMonth = date('d/m/Y', (time() + (30 * 24 * 60 * 60)));

if(isset($_POST) && !empty($_POST)) {
        $description = isset($_POST['description']) && !empty($_POST['description']) ? $_POST['description'] : '';
        
        $files = array();

        if(isset($_FILES) && !empty($_FILES)) {
                foreach($_FILES as $file) {
                        if(isset($file['name']) && !empty($file['name'])) {
                                array_push($files, $file);
                        }
                }
        }
        $receiveDate = isset($_POST['receivedate']) && !empty($_POST['receivedate']) ? $_POST['receivedate'] : '';

        if(!empty($receiveDate)) {
                $explodedDate = explode('/', $receiveDate);

                $day = $explodedDate[0];
                $month = $explodedDate[1];
                $year = $explodedDate[2];
                
                $timestamp = mktime(null, null, null, $month, $day, $year);
                $receiveDate = date('Y-m-d', $timestamp);
        }

        $project = new Project($description, $files, $receiveDate);
        $project->insert();
}
?>
<html>
        <head>
        </head>
        <body>
                <form method="post" enctype="multipart/form-data">
                        <fieldset>
                                <label for="description">Description</label>
                                <textarea id="description" name="description"></textarea>
                                <fieldset>
                                        <label for="file1" />File</label>
                                        <input id="file1" name="file1" type="file" />
                                        <label for="file2" />File</label>
                                        <input id="file2" name="file2" type="file" />
                                        <label for="file3" />File</label>
                                        <input id="file3" name="file3" type="file" />
                                        <label for="file4" />File</label>
                                        <input id="file4" name="file4" type="file" />
                                </fieldset>
                                <label for="receivedate">Receive Date</label>
                                <input id="receivedate" name="receivedate" type="text" value="<?php echo isset($nextMonth) && !empty($nextMonth) ? $nextMonth : null ?>" />
                                <input id="submit" name="submit" type="submit" />
                        </fieldset>
                </form>
        </body>
</html>
I tried to insert dates like: 2010-05-14, 2010-04-14.
But MySQL stores: 0000-00-00.

Somebody can help-me? Thank you!

Re: PDO and MySQL date field

Posted: Thu Apr 15, 2010 4:05 pm
by mikosiko
did you try to insert the fields like dd/mm/yyyy ?

Re: PDO and MySQL date field

Posted: Fri Apr 16, 2010 5:10 am
by proveyourselfthom
No, i didn't. I'm at home now, but i will try at work.
I'll be back to feedback. Thank you!

Re: PDO and MySQL date field

Posted: Fri Apr 16, 2010 6:14 am
by proveyourselfthom
Problem solved!
Instead of:

Code: Select all

$statement = $database->prepare("INSERT INTO projetos (`descricao`, `data_recebimento`) VALUES ('?', '?')");
the right is:

Code: Select all

$statement = $database->prepare("INSERT INTO projetos (`descricao`, `data_recebimento`) VALUES (?, ?)");
PDO takes care of apostrophes! :)
Thank you!