Can't send date to database
Posted: Fri Jul 27, 2007 3:30 pm
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
addToDB.php
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>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>