473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem in saving date in database and displaying

I want to use 2 jumpmenu to let users select publishing date of their document(which only year is necessary to be filled ) and then I will save it in a correct format into db and display what the user filled such as: April 2010,2010,...
here is my code but it doesn't get the date and in my database it is saved 0000-00-00:
FORM:
Expand|Select|Wrap|Line Numbers
  1. <form action="publication.php"  method="post" enctype="multipart/form-data">
  2. <table cellpadding=0 class="text" >
  3. <tr><td>Publisher:</td>
  4. <td><input type="text"  name="publisher"></td></tr>
  5. <tr><td>Date</td>
  6. <td><?php 
  7. /******* building jump menu for the month********/
  8. $today=time();
  9. $monthName = array(1=> "January", "February", "March","April",  "May", "June",  "July", "August", "September", "October","November", "December");
  10. echo "<select name='dateMO'>\n";
  11. for ($n=1;$n<=12;$n++)
  12. {echo "<option value=$n\n";
  13. echo "> $monthName[$n]\n";}
  14. echo "</select>";
  15. //********Building jump menu for year******//
  16. $startYr = date("Y", $today); //get the year from $today
  17. echo "<select name='dateYr'>\n";
  18. for ($n=$startYr-10;$n<=$startYr+1;$n++)
  19. {echo  "<option value=$n";
  20. echo "> $n\n";}
  21. echo "</select>\n";
  22. $date = $_POST['dateYr']."-";
  23. $date .= $_POST['dateMO']."-";
  24. $date .= NULL; //to put 00 at the end of date to be saved in db correctly ?> </td></tr>
  25. <tr><td><input type="submit" id="add" value="add" name="submit"></td> </table></form> 
THE RELATED PHP CODE:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_POST['publisher'])) {
  3. $query=mysql_query("INSERT INTO publication(publisher,date)
  4. VALUES ('{$_POST['publisher']}','$date')") or die(mysql_error());
  5. //displaying :
  6. $respublic= mysql_query("SELECT * FROM publication");
  7. while($rowpublic=mysql_fetch_assoc($respublic))
  8. {echo ($rowpublic["publisher"]). ',' . ($date); 
  9. echo '<p>&nbsp;</p>';}
TABLE:
Expand|Select|Wrap|Line Numbers
  1.  CREATE TABLE publication(
  2.   id int unsigned NOT NULL AUTO_INCREMENT,
  3.    publisher varchar(100) NOT NULL,
  4.    date DATE NOT NULL,
  5.     PRIMARY KEY  (id)
  6. );
I also tested it with 3 jumpmenu(instead of NULL) but it doesn't work either. what was my mistake?only date is not saved and displayed
Apr 10 '10 #1
7 1769
Atli
5,058 Expert 4TB
Hey.

Is the form (your first snippet) and the PHP code (the second snippet) in the same file?

I ask because in your second snippet, you use a $date variable for the INSERT query, but you do not define it in that snippet, only in the first snippet. - To use the variable in the second snippet, it needs to be defined there.

P.S.
Your code is wide open to SQL Injection. In short, you should always use mysql_real_escape_string on all strings and dates before adding them to a SQL query.
Apr 10 '10 #2
yes,the first and second snippet are in one file(publication.php)
Apr 10 '10 #3
Atli
5,058 Expert 4TB
Ok, try replacing the second snippet with this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_POST['publisher'])) 
  3. {
  4.     // Fetch the publisher. You should ALWAYS do this
  5.     // when handling user input. Never use the $_POST
  6.     // data directly in a query.
  7.     $publisher = mysql_real_escape_string($_POST['publisher']);
  8.  
  9.     // Insert the publisher
  10.     $sql = "INSERT INTO publication(publisher,date)
  11.             VALUES ('{$publisher}','$date')";
  12.     $query=mysql_query($sql) or die(mysql_error());
  13.  
  14.     # var_dump($sql); exit;
  15.  
  16.     // Display all
  17.     $respublic = mysql_query("SELECT * FROM publication");
  18.     while($rowpublic=mysql_fetch_assoc($respublic))
  19.     {
  20.         // Note that I replaced $date with $row['date'] here.
  21.         // I assume you wanted to print the date you retrieved
  22.         // from the database, not the one you constructed earlier.
  23.         echo $rowpublic["publisher"]. ',' . $row['date'];
  24.         echo '<p>&nbsp;</p>';
  25.     }
  26. ?>
It's basically the same code you have, just formatted properly and with a few minor fixes. (They are explained in the comments in the code.)

If the code is still not working after you make these modifications, try uncommenting the code on line #14. It will print the query so we can see exactly what you are telling the SQL server to do. If the data is being incorrectly added, this is where the problem is.

Also note the formatting of the code; how I indent it. Makes it easier to read, don't you think? It's extremely important to format the code properly so you can easily read through it later; In case you ever need to hunt for a bug or modify the functionality.
Apr 10 '10 #4
I changed the code but i it still displays 0000-00-00 .i also checked var dump_sql and understood that NULL is entering into db
Apr 11 '10 #5
Atli
5,058 Expert 4TB
What did var_dump print, exactly?

From what you've told me, the likely reason why this is failing is because the $date variable isn't defined. Try moving the part of your first code snippet where it is created into the second snippet (or the one I posted).
Apr 11 '10 #6
hi,thanks for the help,It would print NULL, I also put print for $date but it would print the correct time. I moved the code before sql query and also replaced NULL with 00 for inserting day.it is now ok. only one small problem is left that because i want to ignore day i will do as follows:

Expand|Select|Wrap|Line Numbers
  1. $respublic= mysql_query("SELECT * FROM publication");
  2. while($rowpublic=mysql_fetch_assoc($respublic))
  3. {
  4.      $timestamp=$rowpublic["date"];
  5.      //print ($timestamp);--> it prints the correct time like 2010-04-00 
  6.      $newdate=date("F Y",$timestamp);// i expect it to print April 2010
  7.     //print ($newdate); for any date it prints January 1970  
  8.     echo ($rowpublic["publisher"]). ',' . ($newdate); 
  9.     echo '<p>&nbsp;</p>';
  10. }
Apr 11 '10 #7
I replaced the inner code with
Expand|Select|Wrap|Line Numbers
  1. $timestamp=$rowpublic["date"];
  2. $t=strtotime($timestamp);
  3. $newdate=date('F Y',$t);
  4. echo ($rowpublic["publisher"]). ',' . ($t);  
  5.  
and it is correct now
Apr 11 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
5
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
2
by: Jeronimo Bertran | last post by:
Hello, I am using a rendering aspx page to display a database image on an Image control. When the database record is retrieved, I am saving the bitmap to a session variable for the rendering...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
6
by: yoshitha | last post by:
hi db : sql server 2000 lan : C#.net(ASp.Net) in my database table there are 2 fileds of data type datatime. in field 1 i'm storing date in field 2 i'm storing time.
2
by: jessDMiller | last post by:
I have no clue why the data from the form isn't saving into the database. What am I doing wrong? addAnnounce.php: <td align=top> <form action="index2.php" method="post">Heading:<br...
7
by: arun | last post by:
Hi I am making a booking for a tour. The tours are allowed on some days only( one on monday, other on tuesday like that). For the booking object saving, I am using a wizrd control. In the...
2
by: keerthisreenu | last post by:
hai to all...!! iam working with Ms Access 2000. iam saving an image in the database. After that i want to retrive the same image from database and displaying it in the picture box. but its not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.