Jump to content

need help with date function


doforumda

Recommended Posts

i have following form for date

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="date.php">
  <label>Date:
  <select name="day" id="day">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
  </label>
  -
  <label>
  <select name="month" id="month">
    <option value="1">Jan</option>
    <option value="2">Feb</option>
    <option value="3">Mar</option>
    <option value="4">Apr</option>
    <option value="5">May</option>
  </select>
  </label> 
  -
  <label>
  <select name="year" id="year">
    <option value="2000">2000</option>
    <option value="2001">2001</option>
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2004</option>
  </select>
  </label>
  <p>
    <label>
    <input type="submit" name="button" id="button" value="Submit">
    </label>
  </p>
</form>
</body>
</html>

 

and here is the php page

 

<?php
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];

$date = date($day." ".$month." ".$year);
//$date = date("Y m d");
echo $date;
$db = mysql_connect("localhost");
mysql_select_db("test", $db);
$query = "insert into date
						(
							date
						) 
							values
						(
							'".$date."'
						)";
//echo $query;
$result = mysql_query($query) or die(mysql_error());

echo "your date is added";
?>

 

when i select any date from form and press submit button it adds only this 0000-00-00 to my table. i want date to be inserted to my table not 0s. how can i solve this problem

Link to comment
https://forums.phpfreaks.com/topic/177743-need-help-with-date-function/
Share on other sites

is your column a datetime/date/timestamp type? if so you are going to have to set it so that the default isn't 000-00-00 or something like that. I don't remember exactly what the attribute of the column was, but I had a similar problem a while back. if you have phpmyadmin, you should be able to go into the structure there, and you can figure it out from there. someone else may able to give a more specific solution

 

Isn't it suppose to be: INSERT not insert?

 

doesn't matter

The main parameter of the date() function is a format string, not a date or parts of a date.

 

However, a mysql DATE data type will accept a value as long as the fields are a formated similar to yyyy-mm-dd and since you already have the three parts of the date, why not just validate that a correct date was entered (see checkdate) and then form a yyyy-mm-dd out of the parts and use that in your query. I recommend using a - separator character because a space won't work without leading zeros, whereas yyyy-m-d will work.

 

Edit: And none of the msyql keywords are case sensitive.

use int 15

<?php
$date = $_POST["day"];
$date. = $_POST["month"];
$date. = $_POST["year"];

$date = strtodate($day." ".$month." ".$year);
//$date = date("Y m d");
echo $date;
$db = mysql_connect("localhost");
mysql_select_db("test", $db);
$query = "insert into date
                     (
                        date
                     ) 
                        values
                     (
                        '".$date."'
                     )";
//echo $query;
$result = mysql_query($query) or die(mysql_error());

echo "your date is added ".date("d-m-y",$date)." ";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.