Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I add a "date" value to MySQL table using PHP? 1

Status
Not open for further replies.
Hello everyone,
Table "hourShifts" contains a field names: "shiftdate". Please see table structure attached in annex a.
to_tek-tips1_qo6baj.gif

My code goes like this :
Code:
<?php //MyINITIAL.PHP 
$MyHOST = 'localhost'; 
$MyUSER = 'aaa'; 
$MyPASS = 'bbb'; 
$MyDB = 'test'; 
?>]
Code:
 <?php //MyLOGIN.PHP 
$MyCONNECTION = NEW MYSQLI($MyHOST,$MyUSER,$MyPASS,$MyDB); 
IF(!$MyCONNECTION) DIE(MYSQLI_CONNECT_ERROR()); 
MYSQLI_SET_CHARSET($MyCONNECTION,'UTF8'); ?>]
Code:
<?php // 222.php 
REQUIRE_ONCE 'MyINITIAL.php'; 
REQUIRE_ONCE 'MyLOGIN.php'; 
IF(ISSET($_POST['MyDAT'])) 
{ 
$yosDAT = get_post($MyCONNECTION,'MyDAT'); 
$MyQUERY = "INSERT INTO hourShifts(shiftdate) VALUES($yosDAT)"; 
if(!$MyCONNECTION->query($MyQUERY)) echo '!!!'; } 
echo <<<_END 
<FORM action = "222.php" method = "POST"> 
Shift Date <INPUT TYPE = "date" name = "MyDAT"> 
<input type = "submit" value = "ADD SHIFT"> 
</FORM> 
_END; 
function get_post($MyCONNECTION, $var) 
{ 
return $MyCONNECTION->real_escape_string($_POST[$var]); 
} 
?>]
Here is my display after I added date:
to_tek-tips2_b5enb9.gif

When I want to see the new data in my table I get this:
to_tek-tips3_pl3dam.gif

I wonder if anyone could explain why the new date was not added to my table. I did try to reverse it (dd-mm-yyyy->yyyy-mm-dd) but that didn't work either.
Thanks
 
So what does the query you are running end up looking like once the values are substituted?

Do you get any errors after running the insert statement? You should be since you are not quoting your date string.

The MYSQL online manual has this to say about date fields:

DD-MM-YYYY is not a valid date format for MYSQL. YYYY-MM-DD should work.

So:

$MyQUERY = "INSERT INTO hourShifts(shiftdate) VALUES([highlight #A40000]'[/highlight]$yosDAT[highlight #A40000]'[/highlight])";










----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Simple as it was, it solved my problem ! Thanks a lot !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top