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: - <form action="publication.php" method="post" enctype="multipart/form-data">
-
<table cellpadding=0 class="text" >
-
<tr><td>Publisher:</td>
-
<td><input type="text" name="publisher"></td></tr>
-
<tr><td>Date</td>
-
<td><?php
-
/******* building jump menu for the month********/
-
$today=time();
-
$monthName = array(1=> "January", "February", "March","April", "May", "June", "July", "August", "September", "October","November", "December");
-
echo "<select name='dateMO'>\n";
-
for ($n=1;$n<=12;$n++)
-
{echo "<option value=$n\n";
-
echo "> $monthName[$n]\n";}
-
echo "</select>";
-
//********Building jump menu for year******//
-
$startYr = date("Y", $today); //get the year from $today
-
echo "<select name='dateYr'>\n";
-
for ($n=$startYr-10;$n<=$startYr+1;$n++)
-
{echo "<option value=$n";
-
echo "> $n\n";}
-
echo "</select>\n";
-
$date = $_POST['dateYr']."-";
-
$date .= $_POST['dateMO']."-";
-
$date .= NULL; //to put 00 at the end of date to be saved in db correctly ?> </td></tr>
-
<tr><td><input type="submit" id="add" value="add" name="submit"></td> </table></form>
THE RELATED PHP CODE: - <?php
-
if (isset($_POST['publisher'])) {
-
$query=mysql_query("INSERT INTO publication(publisher,date)
-
VALUES ('{$_POST['publisher']}','$date')") or die(mysql_error());
-
//displaying :
-
$respublic= mysql_query("SELECT * FROM publication");
-
while($rowpublic=mysql_fetch_assoc($respublic))
-
{echo ($rowpublic["publisher"]). ',' . ($date);
-
echo '<p> </p>';}
TABLE: - CREATE TABLE publication(
-
id int unsigned NOT NULL AUTO_INCREMENT,
-
publisher varchar(100) NOT NULL,
-
date DATE NOT NULL,
-
PRIMARY KEY (id)
-
);
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
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.
yes,the first and second snippet are in one file(publication.php)
Atli 5,058
Expert 4TB
Ok, try replacing the second snippet with this: - <?php
-
if (isset($_POST['publisher']))
-
{
-
// Fetch the publisher. You should ALWAYS do this
-
// when handling user input. Never use the $_POST
-
// data directly in a query.
-
$publisher = mysql_real_escape_string($_POST['publisher']);
-
-
// Insert the publisher
-
$sql = "INSERT INTO publication(publisher,date)
-
VALUES ('{$publisher}','$date')";
-
$query=mysql_query($sql) or die(mysql_error());
-
-
# var_dump($sql); exit;
-
-
// Display all
-
$respublic = mysql_query("SELECT * FROM publication");
-
while($rowpublic=mysql_fetch_assoc($respublic))
-
{
-
// Note that I replaced $date with $row['date'] here.
-
// I assume you wanted to print the date you retrieved
-
// from the database, not the one you constructed earlier.
-
echo $rowpublic["publisher"]. ',' . $row['date'];
-
echo '<p> </p>';
-
}
-
}
-
?>
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.
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
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).
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: - $respublic= mysql_query("SELECT * FROM publication");
-
while($rowpublic=mysql_fetch_assoc($respublic))
-
{
-
$timestamp=$rowpublic["date"];
-
//print ($timestamp);--> it prints the correct time like 2010-04-00
-
$newdate=date("F Y",$timestamp);// i expect it to print April 2010
-
//print ($newdate); for any date it prints January 1970
-
echo ($rowpublic["publisher"]). ',' . ($newdate);
-
echo '<p> </p>';
-
}
I replaced the inner code with - $timestamp=$rowpublic["date"];
-
$t=strtotime($timestamp);
-
$newdate=date('F Y',$t);
-
echo ($rowpublic["publisher"]). ',' . ($t);
-
and it is correct now
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |