simonTifo
simonTifo

Reputation: 307

cannot save date in mysql datatase with php

I have a problem with date in php even if I fill the textbox of the date , in the database I find it empty

here is my php page :

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

and here is the coresponding method in 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);
}

I use the datepicker query component for date can you detect me the location of the error thanks

Upvotes: 0

Views: 487

Answers (1)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14302

I think you have given a wrong name to input element. Replace below

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

With

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

You are using $_POST["dueDate"] to get date value and the name is incorrect in your markup.

Edit ::

As @simonTifo said in comment "it return me the exact date, bit in the datatabase it saves like 00-00-0000", there might be some format related issue to overcome this problem just use the date function in php. So the code suppose to be :

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

Check that function manual and set whatever format according to your need.

Hope this will help !!

Upvotes: 1

Related Questions