Can someone help me entering a date "manually" in MySQL thanks :) here's my code it's currently entering 0000-00-00 in my databse

THE HTML

<html>
<head>
<title>..</title>
<body>
<form action="work1.php" method="post">
Value1: <input type="text" name="date"><br>
<input type="Submit">
</form>
</body>
</head>
</html>

PHP

<?php
$username="root";
$password="";
$database="wapwap";

$date=$_POST['Value1'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO date VALUES
('','$date')";

mysql_query($query);

mysql_close();
?>

NEW to PHP :)

Your Date is coming as a String.Due to this , it is displaying 0000-00-00.

Solution is.....

$date=$_POST['Value1'];

---- This is the String.
After taking the date value as a string convert the string with date format.

Member Avatar for rajarajan2017

You should use the name of the element not the label

Day:<input type="text" name="dy"><br>
Month:<input type="text" name="mth"><br>
Year:<input type="text" name="yr"><br>

in PHP

$dy = $_POST['dy'];
$mth=$_POST['mth'];
$yr = $_POST['yr'];
$phpdate = $yr."-".$mth."-".$dy;
$phpdate=date($phpdate);

@Ankit Parmar: Exactly how will i do it? i've searched strtotime() but can you show me how?

After taking the date value as a string convert the string with date format.

@rajarajan07: What I wanted to happen is that to enter a date (i.e. 2010-07-29 which is yyyy-mm-dd) in the textbox and that it will instantly be inserted in MySQL. Well is that ever possible, or I really do need to have separate textboxes for month day and year?

Thanks Experts.

Here's my current code found something on the internet about strtotime, and yes it's entring a data in my database but... but it's 01/01/1970 why is that?

<?php
$username="root";
$password="";
$database="matuto";

//$date=$_POST['Value1'];

$new_date = date("m/d/Y", strtotime($date));

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO datemonga VALUES
('','$new_date')";

mysql_query($query);

mysql_close();
?>

//$date=$_POST; --- Don't comment this line.Because you want to store this value.

$new_date = date("m/d/Y", strtotime($date)); In This line , What is the value of $date. Becoz,You are commenting Previoue line.

That'why , it is storing 01-01-1970(this is initial date value).

I'm trying to get my input in my textbox, the one on my html code

Value1: <input type="text" name="date">

Of course, inserting manually in my php code will work but I wanted to get the value that I inserted in my textbox and upon clicking SUBMIT it will store the date in my database

am i doing something wrong, thanks for the help

<html>
<head>
<title>..</title>
<body>
<form action="work1.php" method="post">
Value1: <input type="text" name="date"><br>
<input type="Submit"/>
</form>
</body>
</head>
</html>
<?php
$username="root";
$password="abcdef";
$database="test_date";
$date=$_POST["date"];  // Your First Problem Post value
echo $date;
echo "i m here";
$date = date("Y/m/d", strtotime($date));   // 2nd Problem Date Format
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO date VALUES('','$date')";

mysql_query($query);

mysql_close();
?>

It's Working .

Change according to this.

Thank You Sir Ankit_Parmar it's now working!!

ok, i had a question for you. Do you want to insert into database the current date...?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.