Page 1 of 1

Can't send date to database

Posted: Fri Jul 27, 2007 3:30 pm
by trassalg
I've got the 2 following programs.. the user first enters "addArticle.php" then upon hitting Submit, goes to the "addToDB.php" program. Login to the server (which is local) works, data is written to the database but to the dateOfArticle column (set as "date" format in the database) just enters "0000-00-00" when I hit Submit. However, hitting "Submit" again and it enters the value I entered in the pulldown menu in the prior screen. Any ideas why? Any fixes?

addArticle.php

Code: Select all

<html>
<head>
<title>Baso de datos de Rodolfo</title>
<link href="styles/styles.css" rel="stylesheet" type="text/css">
<?php
	include("includes/menuBar.php");

	$ThisYear = date("Y");
	setlocale(LC_TIME, 'es_ES');
?>
<?  if (isset($_POST['month']) && is_numeric($_POST['month']) &&
    ((int)$_POST['month'] >= 1 && (int)$_POST['month'] <= 12)) {
    $month = (int)$_POST['month'];
  } else {
    $month = date('n');
  }
  if (isset($_POST['year']) && is_numeric($_POST['year']) &&
    ((int)$_POST['year'] >= 2005 && (int)$_POST['year'] <= 2010)) {
    $year = (int)$_POST['year'];
  } else {
  $year = date('Y');
  }
?>
  <form method="post" action="addToDB.php">
	<table width="90%" align="center">
  	<tr>
    	<td>Date:</td>
      <td>
    <select name="day"><?php
      $maxdays = date('t', mktime(12, 0, 0, $month, 1, $year));
      for ($i = 1; $i <= $maxdays; $i++) {
        if (isset($_POST['day']) && $_POST['day'] == $i) {
          $sel = ' selected';
        } elseif ($i == date('d')) {
          $sel = ' selected';
        } else {
          $sel = '';
        }
        echo "<option value=\"$i\"$sel>$i</option>\n";
      }
    ?></select>

 <select name="month"><?php
      for ($i = 1; $i <= 12; $i++) {
        if ($month == $i) {
          $sel = ' selected';
        } else {
          $sel = '';
        }

        $monthname = strftime('%B', mktime(12, 0, 0, $i, 1, 2005));
        echo "<option value=\"$i\"$sel>$monthname</option>\n";
      }
?>
</select>
    <select name="year"><?php
      for ($i = 2000; $i <= $ThisYear; $i++) {
        if ($year == $i) {
          $sel = ' selected';
        } else {
          $sel = '';
        }
        echo "<option value=\"$i\"$sel>$i</option>\n";
      }
	    ?>
  	</select>
    <?php $dateOfArticle = $year.'-'.$month.'-'.$day; ?>
    <input type='hidden' name='dateOfArticle' value=<?php echo $dateOfArticle; ?>>
			</td>
    </tr>
		<tr>
			<td>Categories:</td>
			<td><input type='text' name='categories'></td>
		</tr>
		<tr>
			<td>Title:</td>
			<td><input type='text' name='articleTitle'></td>
		</tr>
		<tr>
			<td>Text:</td>
			<td><input type='text' name='articleText'></td>
		</tr>
		<tr>
			<td></td>
			<td><input type='submit' value='Submit article'></td></tr></table></form>
</body>
</html>
addToDB.php

Code: Select all

<html>
<head>
<title></title>
<link href="styles/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
include("addArticle.php");
include("includes/misc.inc");
$articleIDNumber=$_POST['articleIDNumber']; 
$dateOfArticle=$_POST['dateOfArticle']; 
$categories=$_POST['categories']; 
$articleTitle=$_POST['articleTitle']; 
$articleText=$_POST['articleText']; 
$logoURL=$_POST['logoURL']; 

include("includes/connection.inc");

$query = "INSERT INTO articleTable (articleIDNumber, dateOfArticle, categories, articleTitle, articleText) VALUES ('','$dateOfArticle','$categories','$articleTitle','$articleText')";
	if (!$query) {
			die ('Could not retrieve data from form : ' . mysql_error());
			}
	
	if (!$dateOfArticle) {
			die ('<br><br><br><br><br>Sorry, no article date submitted');
			}
	else {
			echo ("Queres agregar otro articulo?<br>");
			}

$result = mysql_query($query);

echo $query;

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

mysql_close();


?>
</body>
</html>

Posted: Fri Jul 27, 2007 4:26 pm
by califdon
You're trying to use PHP code to set the date before the user has even entered it. Remember that PHP code is executed on the server before the HTML page is ever sent to the browser. So your code:

Code: Select all

<?php $dateOfArticle = $year.'-'.$month.'-'.$day; ?>
<input type='hidden' name='dateOfArticle' value=<?php echo $dateOfArticle; ?>>
can't possibly supply the value after the page is first sent to the browser. You'll have to do that part in Javascript. I didn't take the time to examine your script to confirm this, but probably your POST variables are passing that value on to the next time the script executes, producing the odd result that it updates it the next time.

Posted: Fri Jul 27, 2007 4:30 pm
by trassalg
But doesn't submitting the form handle that function of entering the date variable, which then in turn would send it to the database?

Posted: Fri Jul 27, 2007 5:12 pm
by RobertGonzalez
If the page loads for the first time, $dateOfArticle would be set today's year-today's month-NULL. I can't see where the day is set in your code.

Before panicing, I would probably tend to echo the SQL out so I can see what the database server is seeing. Can you echo the query that you say is not working and post it so we can see the query that MySQL is seeing? Thanks.

Posted: Fri Jul 27, 2007 5:57 pm
by trassalg
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I've updated the code from the "addArticle.php" program to include the Day selection.... the following is what I added there:

Code: Select all

if (isset($_POST['day']) && is_numeric($_POST['day']) &&
    ((int)$_POST['year'] >= 31 && (int)$_POST['day'] <= 31)) {
    $day = (int)$_POST['day'];
  } else {
  $day = date('d');
  }
Echoing the $query, I see the following (which seems to be correct):

Code: Select all

INSERT INTO articleTable (articleIDNumber, dateOfArticle, categories, articleTitle, articleText) VALUES ('','2007-7-27','a','b','c')
It's working now, accepting today's date into the database in the dateOfArticle column, but how can I change the code so that if the user doesn't want this default selection and would rather change the date options and Submit it via a form, that selected date will be sent to the database rather than the default date?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 29, 2007 1:00 am
by trassalg
How can I change the code so that if the user doesn't want this default date selection and would rather change the date options and Submit it via a form, that selected date will be sent to the database rather than the default date?

allow user to select the date

Posted: Sun Jul 29, 2007 1:26 am
by tansoft
Its simple, just all user to select the date in the form..... this you can do by giving him three combo i.e day,year and month and let user select the date by himself.

Posted: Sun Jul 29, 2007 2:27 am
by trassalg
Is there a way to do this, but also making the 3 pulldown menus automatically reflect today's date (which will be the default entry 90% of the time)?

Posted: Sun Jul 29, 2007 8:18 am
by feyd
Maybe this would be of interest.

Posted: Sun Jul 29, 2007 4:37 pm
by trassalg
Feyd,

Thanks for the link. That script is perfect. Just trying to figure out now how to have the exported format be numerical values, formatted "yyyy-mm-dd" to incorporate it with the php script so it can send to and query the mysql server. I posted in the respective forum, but wanted to let you know it seems to have solved my problem. Thanks for the help.