Page 1 of 1

Problem adding date

Posted: Thu Jul 08, 2010 3:19 am
by tito85
Hi

I am using this code to a movie to my database. However I am having problems adding the dates of the MovieReleaseDate and DVDReleaseDate.

Any help about what I have wrong please?

Thanks

Code: Select all

<?php
  //Connecting with the Database
  require('dbconnect.php');
  //Start up PHP session
  session_start();
  include('securitycheck.php');
  if ($isAdmin) {
      if (isset($_POST['btnCancel'])) {
          header('Location: movies.php');
      } elseif (isset($_POST['btnAddMovie'])) {
          $title = $_POST['txtTitle'];
          $director = isset($_POST['Director']) ? $_POST['Director'] : array();
          $genre = isset($_POST['Genres']) ? $_POST['Genres'] : array();
          if (strlen(trim($title)) > 0 && !empty($director) && !empty($genre)) {
              if(!empty($_POST['txtMovieReleaseDate'])) {
                  $moviereleasedate = date("Y-m-d", strtotime($_POST['txtMovieReleaseDate']));
              } else {
                  $moviereleasedate = '';
              }
			  if(!empty($_POST['txtDVDReleaseDate'])) {
                  $dvdreleasedate = date("Y-m-d", strtotime($_POST['txtDVDReleaseDate']));
              } else {
                  $dvdreleasedate = '';
              }
              $image = $_FILES['txtImage'];
              $rating = $_POST['ddlRating'];
              $review = $_POST['txtReview'];
              $filename = "NoImage.jpg";
              //This will check if an Image was Uploaded
              if (!empty($image['name'])) {
                  //This will check if the uploaded Image is .JPG
                  if ($image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg") {
                      $filename = $image['name'];
                      //This will Upload the Image to the Movie Images Folder
                      move_uploaded_file($image['tmp_name'], "Images/Movies/" . $image['name']);
                  } else {
                      $message = "Only <b>.jpg</b> Images can be uploaded";
                  }
              }
              $insert = "INSERT INTO movies (Title, Review, Rating, MovieReleaseDate, DVDReleaseDate, Filename) VALUES ('" . mysql_real_escape_string($title) . "', '" . mysql_real_escape_string($review) . "', '" . $rating . "', '" . $moviereleasedate . "', '" . $dvdreleasedate . "', '" . $filename . "')";
              mysql_query($insert) or die(mysql_error());
              $LastMovieID = mysql_insert_id();
          } 
      }
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="CSSWebStyles/style.css" />
<link rel="shortcut icon" href="Images/filmicon.ico"/>
<title>Add Movie</title>
</head>
<body>
<div id="website">
<?php
//This will include the code for the banner & navigation
  include('banner.php');
?>
<?php
//This will check if the user is an administrator
  if (isset($_SESSION['superadmin'])) {
      if ($_SESSION['superadmin'])
          include('adminmenu.php');
  }
?>
<div id="content">
<center>
<br />
<h2>Add Movie</h2>
<br />
</center>
<div id="contentgeneral">
<?php
  if (isset($message)) {
      echo "<center>$message</center>";
      unset($message);
  }
?>
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
Title
</td>
<td>
<input type="text" style="width: 300px;" name="txtTitle" />*
</td>
</tr>
<tr>
<td valign="top">
Movie Release Date
</td>
<td>
<input type="text" style="width: 100px;" name="txtMovieReleaseDate" /><small>Date Format: (dd/mm/yyyy)</small><br />
</td>
</tr>
<tr>
<td valign="top">
DVD Release Date
</td>
<td>
<input type="text" style="width: 100px;" name="txtDVDReleaseDate" /><small>Data Format: (dd/mm/yyyy)</small><br />
</td>
</tr>
<tr>
<td valign="top">
Review
</td>
<td>
<textarea name="txtReview" rows="10" cols="100"></textarea>
</td>
</tr>
<tr>
<td valign="top">
Image
</td>
<td>
<input type="file" name="txtImage" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnAddMovie" value="Add" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="btnCancel" value="Cancel" />
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>

Re: Problem adding date

Posted: Thu Jul 08, 2010 7:17 am
by pbs
Are you getting any error message?

Re: Problem adding date

Posted: Thu Jul 08, 2010 7:44 am
by tito85
Hi,

No, just no data is added.

Thanks

Re: Problem adding date

Posted: Thu Jul 08, 2010 10:46 am
by Jade
Have you checked to see that strtotime is returning a value? Also, have you checked to make sure you're getting into your date if statements?

Re: Problem adding date

Posted: Thu Jul 08, 2010 1:50 pm
by tito85
No however I noticed that sometimes it is working and sometime no.

I really don't know why!

Re: Problem adding date

Posted: Thu Jul 08, 2010 3:58 pm
by mikosiko
tito85 wrote:No however.....
I really don't know why!
well... if you start testing has Jade has suggested previously sure you will find out why.

happy testing

Re: Problem adding date

Posted: Fri Jul 09, 2010 6:28 am
by tito85
What I've noticed is that using some dates is working and using other dates is not. But there is no pattern.

Is there any way maybe to improve the code?

Thanks for any help.

Re: Problem adding date

Posted: Fri Jul 09, 2010 9:22 am
by agriz
If that so, you might need to alter your html form

1) Use some javascript calender.
2) Use the three select box (date, month and year)

I am not sure how to handle this in PHP if the format is different than expected.
I will wait and see for other solutions :-)

Re: Problem adding date

Posted: Fri Jul 09, 2010 9:36 am
by tito85
I don't wish to use other language because I am sure that PHP can handle this.

I need some help to solve this issue but I don't wish to alter a lot of work. I am new to PHP and don't know anything about other languages.

However thanks for you comment.

Re: Problem adding date

Posted: Fri Jul 09, 2010 1:54 pm
by Jade
The problem is that you need to reformat the date. The function is expecting dates like this:

Code: Select all

echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
Not dates like this:

[text]dd/mm/yyyy[/text]

You need to separate the day/month and year out of the string and then you can convert it to a date from there or you need to use a valid date format found here: http://www.php.net/manual/en/datetime.formats.date.php

Re: Problem adding date

Posted: Fri Jul 09, 2010 2:03 pm
by tito85
Hi,

Therefore why is sometimes working and sometimes not?

Re: Problem adding date

Posted: Fri Jul 09, 2010 8:20 pm
by agriz
So lets assume that users enter exactly dd/mm/yyyy

Code: Select all

if(!empty($_POST['txtMovieReleaseDate'])) {
        $moviereleasedate = date("Y-m-d", strtotime($_POST['txtMovieReleaseDate']));
 }
instead of this,

Code: Select all

if(!empty($_POST['txtMovieReleaseDate'])) {
        $txtMovieTempDate = explode("/",$_POST['txtMovieReleaseDate']);
        $txtMovieTempDate = $txtMovieTempDate[2]."-".$txtMovieTempDate[1]."-".$txtMovieTempDate[0];
        $moviereleasedate = date("Y-m-d", strtotime($txtMovieTempDate));
 }
Try this, or you can also use mktime()

But it will only work if the user enters dd/mm/yyyy
We have not had much options if the format is different.

Re: Problem adding date

Posted: Sat Jul 10, 2010 8:28 am
by tito85
Yes the date will be inserted by an admin. And will be only inserted in the format of dd/mm/yyyy.

It seems to be working, therefore thanks very much for your help and time!!!

Will make some tests just to be sure.

Thanks again!