0

即使我填写了日期的文本框,我在 php 中的日期也有问题,在数据库中我发现它是空的

这是我的 php 页面:

    <?php
session_start();
if (!array_key_exists("user", $_SESSION)) {
    header('Location: index.php');
    exit;
}
require_once("Includes/db.php");
$wisherID = WishDB::getInstance()->get_wisher_id_by_name($_SESSION['user']);

$wishDescriptionIsEmpty = false;
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (array_key_exists("back", $_POST)) {
        header('Location: editWishList.php');
        exit;
    } else
    if ($_POST['wish'] == "") {
        $wishDescriptionIsEmpty = true;
    } else if ($_POST["wishID"] == "") {
        WishDB::getInstance()->insert_wish($wisherID, $_POST["wish"], $_POST["dueDate"]);
        header('Location: editWishList.php');
        exit;
    } else if ($_POST["wishID"] != "") {
        WishDB::getInstance()->update_wish($_POST["wishID"], $_POST["wish"], $_POST["dueDate"]);
        header('Location: editWishList.php');
        exit;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link type="text/css" href="jquery-ui-1.8.24.custom/css/smoothness/jquery-ui-1.8.24.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="jquery-ui-1.8.24.custom/js/jquery-1.8.0.min.js"></script>
        <script type="text/javascript" src="jquery-ui-1.8.24.custom/js/jquery-ui-1.8.24.custom.min.js"></script>
        <script type="text/javascript" src="jquery-ui-1.8.24.custom\development-bundle\ui\i18n\jquery.ui.datepicker-fr.js"></script>
        <script type="text/javascript">
            $(function() {
        $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
        $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );

    });         
        </script>
    </head>
    <body>
        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST")
            $wish = array("id" => $_POST["wishID"], "description" => $_POST["wish"], "due_date" => $_POST["dueDate"]);
        else
        if (array_key_exists("wishID", $_GET))
            $wish = mysqli_fetch_array(WishDB::getInstance()->get_wish_by_wish_id($_GET["wishID"]));
        else
            $wish = array("id" => "", "description" => "", "due_date" => "");
        ?>
        <form name="editWish" action="editWish.php" method="POST">
            <input type="hidden" name="wishID" value="<?php echo $wish["id"]; ?>" />
            <table>
                <tr>
                    <td>Describe your wish:</td>
                    <td><input type="text" name="wish"  value="<?php echo $wish['description']; ?>" /></td>
                    <td><?php if ($wishDescriptionIsEmpty) echo "Please enter description"; ?></td>
                </tr>
                <tr>
                    <td>When do you want to get it?</td>

                    <td><input type="text" name="due_date" id="datepicker" value="<?php echo $wish['due_date']; ?>" /></td>
                </tr>                
            </table>
            <input type="submit" name="saveWish" value="Save Changes"/>
            <input type="submit" name="back" value="Back to the List"/>

        </form>
        je suis 
        </br>

    </body>
</html>

这是 db.php 中的对应方法:

function insert_wish($wisherID, $description, $dueDate) {
    $description = $this->real_escape_string($description);
    if ($duedate == '') {
        $this->query("INSERT INTO wishes (wisher_id, description)" .
                " VALUES (" . $wisherID . ", '" . $description . "')");
    } else
        $this->query("INSERT INTO wishes (wisher_id, description, due_date)" .
                " VALUES (" . $wisherID . ", '" . $description . "', '" . $dueDate . "')");
}    

public function update_wish($wishID, $description, $duedate) {
    $description = $this->real_escape_string($description);
    if ($duedate == null) {
        $this->query("UPDATE wishes SET description = '" . $description . "', due_date = NULL WHERE id = " . $wishID);
    } else
        $this->query("UPDATE wishes SET description = '" . $description . "', due_date = '" . $duedate . "' WHERE id = " . $wishID);
}

我使用 datepicker 查询组件获取日期,你能检测到错误的位置吗 谢谢

4

1 回答 1

1

我认为您为输入元素提供了错误的名称。在下面替换

<input type="text" name="due_date" id="datepicker" 
       value="<?php echo $wish['due_date']; ?>" />

<input type="text" name="dueDate" id="datepicker" 
       value="<?php echo $wish['due_date']; ?>" />

您正在使用$_POST["dueDate"]获取日期值并且标记中的名称不正确。

编辑 ::

正如@simonTifo 在评论中所说的“它返回给我确切的日期,它保存在数据库中的位,如 00-00-0000”,可能存在一些与格式相关的问题来克服这个问题,只需使用 php 中的date函数。所以代码假设是:

WishDB::getInstance()->insert_wish($wisherID, $_POST["wish"], 
                              date('Y-m-d H:i:s', $_POST["dueDate"]));

检查该功能手册并根据需要设置任何格式。

希望这会有所帮助!

于 2012-10-06T11:25:57.147 回答