3

I have searched on the above topic and found various answers but didn't help me out whenever I post a date on from ny web form, the result on my table is always 0000-00-00 I have been at this for 2 days and tried all suggestions i found but to no avail. i am sure i'm missing a very tiny detail. here are my codes:

<html>

<head>
<meta charset="utf-8">
<title>IBADAM BMs</title>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">


<script>
$(function() {
$("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
});
</script>


</head>

<body bgcolor="#E6E6FA">

<br>
<div>
    <div style="float: left; margin-left: 340px;>
    <a href="index2.php"><img src="../image/banner.png" alt="" align=""/></a>
</div>

<br><br><br><br><br>

<div style="float: left; margin-left: 1100px; margin-top: 0px;">
<a href="../logout.php">Log out</a>
</div>
<br><br>
<center><b><h3>DIVISION</h3></b></center>

<?php
error_reporting (E_ALL ^ (E_NOTICE + E_WARNING));
$con=mysqli_connect("localhost","alagbeco","a12345","alagbeco_modem");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


if(!isset($_POST['submit'])) {
    ?>

<center>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<br><br>


Branch: 
<select name="branch">
<option></option>
<option>DIVINE</option>
<option>GLORY 1</option>
<option>GLORY 2</option>
</select>

<br><br>

Date: 
<input type="text"  name="datepicker" id="datepicker">
<br><br>


Amount: 
<input type="text" name="amount">
<br><br>

<input type="submit" name = "submit" value ="Process">
</form>
</center>


    <?php



} else {
        // escape variables for security
        $branch = $_POST['branch'];
        $amount = $_POST['amount'];
        $datepicker = $_POST['date'];

        //check for existence
        $check_sql = "SELECT count(no) FROM bm_ibadan_division WHERE branch= '$branch' AND date = '$datepicker'";
        $check = mysqli_query($con,$check_sql);
        while ($check_rsult = mysqli_fetch_array($check)) {
            $count = $check_rsult['count(no)'];
            if($count >0 ) {
                die ("Double treatment not allowed");
            } else {



                    //insert into table
                    $sql = "INSERT INTO bm_ibadan_division (branch, status, amount, date)
                    VALUES ('$branch', 'Reconciled', '$amount', '$datepicker')";
                    $result = mysqli_query($con, $sql);
                }       echo '<br><br><center>Entry Successful</center><br><br>';   
        }   
}

?> 

<br>
</body>
</html>
Fola Idowu
  • 31
  • 1
  • 3
  • Did you try to check what $datepicker looks like? $datepicker may be returning a datetime, not a date, therefore you should convert it to a regular date before querying it. – briosheje Mar 11 '15 at 10:30
  • A string formatted like 'yy-mm-dd' isn't the same as a datetime value in your database.. So unless you write your datetime as a varchar in the DB, you will never get the same result... – Naruto Mar 11 '15 at 10:35
  • same question with the right answer http://stackoverflow.com/questions/12120433/php-mysql-insert-date-format – Stefan Yohansson Mar 11 '15 at 10:40
  • check your data type in which you are saving the date. – Manoj Dhiman Mar 11 '15 at 10:42

5 Answers5

2

Formate should be yy-mm-dd.Try like this:

<script>
$(function() {
$("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
 });
</script>

also change: $datepicker = $_POST['date']; to $datepicker = $_POST['datepicker'];

Priyank
  • 3,778
  • 3
  • 29
  • 48
0

date format has to be YYYY-MM-DD

<script>
$(function() {
$("#datepicker").datepicker({dateFormat: 'yyyy-mm-dd'});
});
</script>
sudhi
  • 41
  • 1
0

Either change this

<input type="text"  name="datepicker" id="datepicker">

to

<input type="text"  name="date" id="datepicker">

OR

Change

$_POST['date']

to

$_POST['datepicker']
Shravan Sharma
  • 989
  • 8
  • 17
  • Yessssssss! it worked! All I had to do was change $_POST['date'] to $_POST['datepicker']. just that little change. THANK YOU! – Fola Idowu Mar 11 '15 at 12:19
0

In your php code

$datepicker=$_POST['date'] like this.

Please Change to $_POST['datepicker']. Then it will works.Thanks

Krishna38
  • 707
  • 1
  • 16
  • 43
0

I also faced this problem and I used following code:

date('Y-m-d', strtotime(str_replace('/', '-', $_POST['date'])))