0

Hi every on.I new in php.I m trying to insert date from 3 dropdwon to my date table.It is working well but in table the date is showing always 00-00-0000.plz any one give me a solution.My scripts are followings:

//table
-- Table structure for table `date`
--

CREATE TABLE IF NOT EXISTS `date` (
  `id` int(5) NOT NULL auto_increment,
  `date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=11 ;
//HTML
<form method="post" action="insert.php">
<select name="Birthday_month" id="Birthday_month">
			<option value="-1">Month:</option>
			<option value="1">Jan</option>		
			<option value="2">Feb</option>
			<option value="3">Mar</option>
</select><select name="Birthday_day" id="Birthday_day">
			<option value="-1">Day:</option><option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
</select>
<select name="birthday_year" id="birthday_year">
<option value="-1">Year:</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
</select>
<input type="submit" name="Signup" value="Sign_Up">
          
</form>
//insert.php
<?php
include('config.php');
if (isset($_POST['Signup']))
{
$timestamp=strtotime($_POST['Birthday_month'].$_POST['Birthday_day'].$_POST['birthday_year']);
$date=date('M-d-Y',$timestamp);
$query="insert into date values('','$date')";
$res=mysql_query($query)or die(mysql_error());
echo mysql_affected_rows();
}


?>
//config.php
<?php 
$server='localhost';
$user='root';
$password='';
$database='test';

$link=mysql_connect($server,$user,$password);
if(!$link)
{
	die ("Failed to Connect".mysql_error());
}

$db_select=mysql_select_db($database,$link);
if(!$db_select)
{
	die("failed select database".mysql_error());
}
?>

//table after insert
id date
1 0000-00-00 00:00:00
2 0000-00-00 00:00:00
3 0000-00-00 00:00:00
4 0000-00-00 00:00:00
5 0000-00-00 00:00:00
6 0000-00-00 00:00:00

4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by cwarn23
0
`date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

Just use the date datatype for the 'date' field.

Collect the value of the dates:

$dy = $_POST['dy'];
$mth=$_POST['mth'];
$yr = $_POST['yr'];
$phpdate = $yr."-".$mth."-".$dy;
$phpdate=date($phpdate);

Then use the insert statement to update the records

0

thank Mr Raja.But its still the out put is

0000-00-00.I have changed data type to 'date' and changed the script as u give...

0

edit your insert.php to this

<?php
include('config.php');
if (isset($_POST['Signup']))
{
$bday = $_POST['birthday_year']."-".$_POST['Birthday_month']."-".$_POST['Birthday_day'];
$query="insert into date values('','".$bday."')";
$res=mysql_query($query)or die(mysql_error());
echo mysql_affected_rows();
}


?>

Edited by vaultdweller123: n/a

0

May I suggest to change the column type to bigint and instead of sending dates to mysql just sent the timestamp integer. Then you may use the php date function to retrieve it in any format.

0

yeah its better save it as timestamp like his original code, but he wants it to be inserted as yyyy-mm-dd formatted date

0

May I suggest to change the column type to bigint and instead of sending dates to mysql just sent the timestamp integer. Then you may use the php date function to retrieve it in any format.

@cwarn23: would uu plz give me some hints about the script as u suggest. As i m just learning php....I dont clearly recognize ur suggestion.Anyway thank u very much ...

0
<?php
$date=time();
mysql_query('INSERT INTO `table` SET `number_column`='.$date) or die(mysql_error());

$r=mysql_query('SELECT * FROM `table`') or die(mysql_error());
$data=mysql_fetch_assoc($r);
echo date('j/n/Y',$date['number_column']);

Edited by cwarn23: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.