Jump to content

Inserting date into MySql Table with PHP


DYWBH

Recommended Posts

Hello, I have created a very basic form that inserts user information to a MySql Table. I am now trying to make it insert the current date into a Date column within the table.

 

Here is my code:

 

<?php


mysql_connect("mysql9.000webhost.com", "a3313201_admin", "xxxxxx") or die(mysql_error()); mysql_select_db("a3313201_admin") or die(mysql_error()); 

$Username = $_POST['Username'];
$Password= $_POST['Password'];


$query="INSERT INTO Users (Username, Password, [b]DateJoin[/b])VALUES('".$Username."', '".$Password."', [b]???[/b])";

mysql_query($query) or die ('Error 101');

echo "Accoount Created";

?>

 

Thank you for your help

 

<?php
mysql_connect("mysql9.000webhost.com", "a3313201_admin", "xxxxxx") or die(mysql_error()); mysql_select_db("a3313201_admin") or die(mysql_error()); 

$Username = $_POST['Username'];
$Password= $_POST['Password'];
$DateJoin= $_POST['DateJoin'];

$query="INSERT INTO Users (Username, Password, DateJoin) VALUES ('".$Username."', '".$Password."', '".$DateJoin."')";
mysql_query($query) or die ('Error 101');

echo "Accoount Created";

?>

 

While in the form where you $_POST the username and password add this as a hidden field:

<input type="hidden" name="DateJoin" id="DateJoin" value="<?php echo date('Y-m-d H:i:s'); ?>" />

 

Now the date('Y-m-d H:i:s'); is to be used if you have the column

DateJoin

in the sql table set as a DateTime.

 

fugix's solution is another way. Both ways work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.