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

pu82cl6c  于 2023-08-04  发布在  jQuery
关注(0)|答案(3)|浏览(101)

这是我的总代码,但问题是我不能在数据库中插入数据的原因日期选择器。与出日期选择器,它的插入成功在数据库中,当总表是空的,那么它的插入成功的第一次,但其他时间它不插入任何东西。谁能帮帮我?

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";
            }
        }
    }
}


超文本标记语言:

<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>

gijlo24d

gijlo24d1#

它可能是一个字符串而不是日期格式,正如你告诉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')";

字符串

6ju8rftf

6ju8rftf2#

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

7fyelxc5

7fyelxc53#

简单地将代码中的'-'替换为'/':

$dtime = date('Y-m-d',strtotime($_POST['dtime'])); \\ Wrong
$dtime = date('Y/m/d',strtotime($_POST['dtime'])); \\ Correct

字符串

相关问题