Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am writing program using php and i faced problem in inserting the date in the database this is the code :
PHP
$e_no= $query_row['e_no'];
$grade= $query_row['e_grade'];
$ccn_code=$query_row['e_ccn'];
echo "<br>"."<br>".""."<FONT COLOR='FFFFF0'>"."Employee Name:".$query_row['e_name']."</font>"."";
  echo ""."<FONT COLOR='FFFFF0'>"."Employee Number:".$query_row['e_no']."</font>"."";
  echo ""."<FONT COLOR='FFFFF0'>"."Employee Design:".$query_row['e_design']."</font>"."";
    if(isset($_REQUEST['submit']))
  { $otn_days=$_POST['date'];//take the date from date input
    $otn_hrs=$_POST['text'];
    $result2 = mysqli_query($connection,"INSERT INTO otn (e_no,grade,ccn_code,otn_days,otn_hrs) values($e_no,$grade,$ccn_code,$otn_days,$otn_hrs) ");
  }}
else
 {die($failconnection);}

after that i check the database , the date column contain this value 0000-00-00.
i check that code separately and give me correct date like 2013-10-01
i do not know where is the problem
Posted
Updated 28-Jan-14 1:18am
v4
Comments
Kornfeld Eliyahu Peter 28-Jan-14 7:20am    
Can you show what $result2 gets?
loai_maane 28-Jan-14 7:30am    
this is example :

e_no grade ccn_code otn_days otn_hrs
2393 07 270 0000-00-00 5
loai_maane 28-Jan-14 7:32am    
i know in mysql Date field is of format yyyy-mm-dd,but i checked the code separately and gave me correct format date like 2013-10-01.
loai_maane 28-Jan-14 7:34am    
and also i used date('Y-m-d', strtotime($_POST['date'])), but same thing the date filed
0000-00-00
Kornfeld Eliyahu Peter 28-Jan-14 7:37am    
First sql stores date in binary format so the representation of it has not too much meaning...
And to my question: mysqli_query returns false on failure or a mysqli_result object - please check what you got!

i find the answer thnx Kornfeld, just put $otn_days into single quote
mysqli_query($connection,"INSERT INTO otn(e_no,grade,ccn_code,otn_days,otn_hrs) values($e_no,$grade,$ccn_code,'$otn_days',$otn_hrs) ");
 
Share this answer
 
When you are composing the SQL query in php you need to submit the date as a string. I cannot tell from your code what you are getting from $_POST['date'],

Let's consider two cases:
1) You're submitting a php datetime object: this would possibly crash your code as for a great many purposes it must first be formatted to be of any use. Since you did insert a row, this is probably not what your doing.
2) the value is in some sort of date format (yyyy-mm-dd, for example) - but you've no single quotes around the value.

(2) seems more like your problem, at least for a start.

Change query to:
<br />
  INSERT INTO otn (e_no,grade,ccn_code,otn_days,otn_hrs)<br />
  values($e_no,$grade,$ccn_code,'$otn_days',$otn_hrs)<br />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900