Jquery日期选择器无法通过php保存在数据库中

时间:2013-12-28 00:26:53

标签: php jquery mysql sql performance

这是我的总代码,但问题是我无法在数据库原因日期选择器中插入数据。没有日期选择器它在数据库中成功插入,当总表为空时,它成功插入第一次但其他时间它没有插入任何东西。请任何人帮助我?


CREATE TABLE IF NOT EXISTS `teacher` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `father_name` varchar(50) NOT NULL,
  `mother_name` varchar(50) NOT NULL,
  `address` text NOT NULL,
  `sex` varchar(50) NOT NULL,
  `position` varchar(150) NOT NULL,
  `subject` varchar(150) NOT NULL,
  `sallary` int(50) NOT NULL,
  `dtime` date NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `position` (`position`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=69 ;

if (isset($_POST['formsubmitted'])) {

    if (isset($_POST['name'])&&isset($_POST['f_name'])&&isset($_POST['m_name'])&&isset($_POST['dtime'])&&
        isset($_POST['sex'])&&isset($_POST['address'])&&isset($_POST['position'])&&isset($_POST['subject'])
        &&isset($_POST['salary'])) {

        $name = $_POST['name'];
        $father =$_POST['f_name'];
        $mother =$_POST['m_name'];
        $sex =$_POST['sex'];
        $address =  $_POST['address'];
        $position = $_POST['position'];
        $subject    =   $_POST['subject'];
        $salary =   $_POST['salary'];
        $dtime = date('Y-m-d',strtotime($_POST['dtime']));

        if (!$dtime) {
            echo $dtime;
        }else{
            $query = "INSERT INTO teacher (name, father_name, mother_name, address, sex, position, subject, salary,dtime)VALUES('$name','$father','$mother','$address','$sex','$position','$subject','$salary',$dtime)";     
            $query_run = mysqli_query($db,$query);
            if ($query_run) {
                echo 'success!';

            } else {
                echo "try again";
            }
        }
    }
}

HTML:

<hr>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<form action="teacher_sallary.php" method="POST">
    Name:<br>
    <input type="text" name='name'required><br><br>
    Father Name:<br>
    <input type="text" name='f_name'required><br><br>
    Mother Name :<br>
    <input type="text" name='m_name'required><br><br>
    Address:<br>
    <textarea rows="7" cols="31" name ='address' >
    Please write your address here !
    </textarea> <br><br>
    Sex:<br>
    <select name="sex">
    <option value="male">Male</option>
    <option value="female">Female</option>
    </select><br><br>
    Position:<br>
    <input type="text" name='position' required><br><br>
    Subject:<br>
    <input type="text" name='subject' required><br><br>
    Sallary:<br>
    <input type="text" name='salary' required><br><br>
    Date:<br>
    <br><p><input type="text" id="datepicker" name="dtime"></p><br>
    <input type="hidden" name=  "formsubmitted" value="TRUE" />

    <input type="submit" value='Submit'>

</form>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

它可能是一个字符串而不是日期格式,因为你已经告诉它应该是mysql。你可以试试这个:

 $query = "INSERT INTO teacher (name, father_name, mother_name, address, sex, position, subject, salary,dtime)VALUES('$name','$father','$mother','$address','$sex','$position','$subject','$salary',str_to_date($dtime, '%Y/%m/%d')";   

答案 1 :(得分:0)

您需要先正确解析日期,例如对于默认日期格式,请使用%m /%d /%Y。