Getting date with jQuery and saving it to MySQL with a PHP form

I need a small example on sending date to mysql db from a php form. Tried lots of examples but I seems I am missing o step. I have prepared a form for 3 fields, name last name and birth date. When I fill the form and submit date filed gets crazy :) 0000-00-00 00:00:00

I tried to format the date but this time I could not understand how to pass it to mysql. Here is my funny code;

<?php include "header.php" ?>
<?php include "conn.php" ?>
<script>
    $(function() {
        $( "#bdate" ).datepicker();
    });
</script>
<?php
    if(isset($_POST['submit'])){
        $PlayerName = $_POST['PlayerName'];
        $PlayerLname = $_POST['PlayerLname'];
        $PlayerBdate = $_POST['PlayerBdate'];
        $query = "INSERT INTO tblPlayer (
                PlayerName, PlayerLname, PlayerBdate
    ) VALUES (
        '{$PlayerName}', '{$PlayerLname}', '{$PlayerBdate}'
    )";
        if(mysql_query($query,$connection)){
            //Sucess
            header("Location: players_list.php");
        } else{
            echo "<p>something is wrong</p>";
            echo "<p>" . mysql_error() . "</p>";
        }

    }
?>
<form action="add_player.php" method="post">
<table>
<tr>
   <td>Name: </td>
   <td><input type="text" name="PlayerName" /></td>
   </tr>
   <tr>
   <td>Lastname:</td>
   <td><input type="text" name="PlayerLastName" /></td>
   </tr>
   <tr>
   <td>Birthdate:</td>
   <td><input id="bdate" type="text" name="PlayerBdate" /></td>
   </tr>
   <tr>
   <td><input type="submit" name="submit" value="Save" /></td>
   <td align="right">&nbsp;</td>
   </tr>
   </table>
<?php ob_end_flush() ?>
<?php include ("footer.php") ?>

header.php contains

<?php ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/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.2/jquery-ui.js"></script>
<title>Untitled Document</title>
</head>
<body>

and conn.php is the standard connection sting file.Footer.php is just closing the connection:) Also I could not make my code work without the ob_start(); and end functions:S

So can you point out how can I get the date as day/month/year format and save it to mysql that way?

Answers


since your date in mysql is in Y-m-d H:i:s format.. use php date() function to convert the date into required format.

replace

$PlayerBdate = $_POST['PlayerBdate'];

with

 $PlayerBdate = date('Y-m-d H:i:s',strtotime($_POST['PlayerBdate']));

Need Your Help

Sequelize OR with null

sql node.js sequelize.js

I am trying to use Sequelize to generate a (semi) complex conditional.

JavaScript - How escape quotes in a var to pass data through Json

javascript json

I'm passing some data through Json to a Webservice.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.