如何在mysql中插入日期

时间:2015-03-03 12:58:54

标签: php mysql

到目前为止,我已经创建了年,月,日的下降。我已经使用php在mysql数据库中插入数据但它不起作用,日期没有插入到数据库中,它真的很烦人,因为我不知道问题是什么。请帮忙。谢谢

表单代码

<form method=post name=f1 action=''><input type=hidden name=todo value=submit>
<table border="0" cellspacing="0" >
<tr><td align=left >
<select name=month value=''>Select Month
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option> etc.. 

我每天都做同样的事情。

php code

require("common.php"); 
<?php
$todo=htmlentities($_POST['todo']);
if(isset($todo) and $todo=="submit"){
$month=htmlentities($_POST['month']);
$dt=htmlentities($_POST['dt']);
$year=htmlentities($_POST['year']);
$date_value="$month/$dt/$year";
echo "mm/dd/yyyy format :$date_value<br>";
$date_value="$year-$month-$dt";
echo "YYYY-mm-dd format :$date_value<br>";
}

$m=$month;
$d=$dt;
$y=$year;
If(!checkdate($m,$d,$y)){
echo "invalid date";
}else {
echo "Entry date is correct";
}
$sql="INSERT INTO survey (dates)
VALUES
('$_POST[$date_value])"; 

     mysql_query($sql);


      exit();  

?>

4 个答案:

答案 0 :(得分:0)

更正您的插入查询:

$sql="INSERT INTO survey (dates) VALUES ('$date_value')"; 

答案 1 :(得分:0)

使用此功能只需查看下面的日期

$month=htmlentities($_POST['month']);
$dt=htmlentities($_POST['dt']);
$year=htmlentities($_POST['year']);
$date_value=$year.'-'.$month.'-'.$dt;

$sql="INSERT INTO survey (dates) VALUES ('".$date_value."')"; 

答案 2 :(得分:0)

$ _ POST [$ date_value]不是$ _POST的一部分!

$date_value="$year-$month-$dt";

所以用$ date_value

替换INSERT队列中的$ _POST [$ date_value] 像这样

$sql="INSERT INTO survey (dates) VALUES ('".$date_value."')"; 

答案 3 :(得分:-3)

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'INSERT INTO employee '.
       '(emp_name,emp_address, emp_salary, join_date) '.
       'VALUES ( "guest", "XYZ", 2000, NOW() )';

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>

这可能会帮助你..