bjokneti
November 4th, 2004, 02:22 PM
I am REALLY NEW to php and mysql, and so this may be simple ignorance on my part. I pulled the majority of the following code from the web and have changed it to work with my database. The connection to the database is made successfully and all available records are shown in the browser. However, I am having two problems:
1) The if statement with isset never allows the form included in the page to be displayed on screen.
2) When I submit data to the database using the form it does not end up in the database, but no error is generated. Have I missed a crucial step? THANKS IN ADVANCE!
Here is the html/php code.
-------------------------------------------
<html>
<head>
<title> The LMG Daily Metrics Database </title>
</head>
<body>
<?php
if (isset($addcode)): // If the user wants to add a joke
?>
<form action="<?=$PHP_SELF?>" method="post">
<p>Date: <input type="text" name="date" /><br />
Code: <input type="text" name="code" /><br />
Comments:<br />
<textarea name="comments" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitcode" value="SUBMIT" /></p>
</form>
<a href="lmg_daily_fromjoke.php">View All Daily Codes</a>
<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "root");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the lmg daily code database
if (! @mysql_select_db("marine_metrics") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a code has been submitted,
// add it to the database.
if ($submitcode == "SUBMIT") {
$sql = "INSERT INTO lmg_daily SET
date='$date',
code='$code',
comments=$'comments'";
if (@mysql_query($sql)) {
echo("<p>One record successfully added.</p>");
} else {
echo("<p>Error adding record: " .
mysql_error() . "</p>");
}
}
echo("<p> Here are the LMG Daily Metrics submitted to date: </p>");
?>
<table width="300"><th colspan="3">LMG 2004-05 Daily Metrics</th>
<tr align=center><td>Date</td><td>Code</td><td>Comments</td></tr>
<?php
//Request all LMG Daily Metrics
$result = @mysql_query('SELECT * FROM lmg_daily');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
// Display the text of each in a table
while ($row = mysql_fetch_array($result))
{
echo '<tr><td align="center">' . $row['date'] . '</td><td align="center">' . $row['dailycode'] . '</td><td align="center">' . $row['comments'] . '</td></tr>';
}
?>
</table>
<?php
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p>Enter today's code by <a href='$PHP_SELF?addcode=1'>clicking here</a></p>");
endif;
?>
</body>
</html>
1) The if statement with isset never allows the form included in the page to be displayed on screen.
2) When I submit data to the database using the form it does not end up in the database, but no error is generated. Have I missed a crucial step? THANKS IN ADVANCE!
Here is the html/php code.
-------------------------------------------
<html>
<head>
<title> The LMG Daily Metrics Database </title>
</head>
<body>
<?php
if (isset($addcode)): // If the user wants to add a joke
?>
<form action="<?=$PHP_SELF?>" method="post">
<p>Date: <input type="text" name="date" /><br />
Code: <input type="text" name="code" /><br />
Comments:<br />
<textarea name="comments" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitcode" value="SUBMIT" /></p>
</form>
<a href="lmg_daily_fromjoke.php">View All Daily Codes</a>
<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "root");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the lmg daily code database
if (! @mysql_select_db("marine_metrics") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a code has been submitted,
// add it to the database.
if ($submitcode == "SUBMIT") {
$sql = "INSERT INTO lmg_daily SET
date='$date',
code='$code',
comments=$'comments'";
if (@mysql_query($sql)) {
echo("<p>One record successfully added.</p>");
} else {
echo("<p>Error adding record: " .
mysql_error() . "</p>");
}
}
echo("<p> Here are the LMG Daily Metrics submitted to date: </p>");
?>
<table width="300"><th colspan="3">LMG 2004-05 Daily Metrics</th>
<tr align=center><td>Date</td><td>Code</td><td>Comments</td></tr>
<?php
//Request all LMG Daily Metrics
$result = @mysql_query('SELECT * FROM lmg_daily');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
// Display the text of each in a table
while ($row = mysql_fetch_array($result))
{
echo '<tr><td align="center">' . $row['date'] . '</td><td align="center">' . $row['dailycode'] . '</td><td align="center">' . $row['comments'] . '</td></tr>';
}
?>
</table>
<?php
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p>Enter today's code by <a href='$PHP_SELF?addcode=1'>clicking here</a></p>");
endif;
?>
</body>
</html>