Click to See Complete Forum and Search --> : Using isset with if/then statement to display a form


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>

bjokneti
November 9th, 2004, 12:49 PM
Is there any other information I can provide to help with answering my question?

Still needing help! :-)

Danii
November 9th, 2004, 01:45 PM
I don't understand where you get $addcode from. I don't think eaven the program understands where you get $addcode from. Do you want the code to be true when pressing a link? Then you should change $addcode to $_GET['addcode']

Viper_SB
November 12th, 2004, 01:14 AM
First if you post your code within either [ php ] or [ code ] tags it keeps the formating and makes it a LOT easier to try to debug for you. (wihout having to copy and paste it out)

Do you have register globals enabled or disabled? The only way $addcode will be set is if you have register globals enabled (strongly recommened that you have register globals DISABLED).

In either case the proper way to get the data would be to use the global variables. i.e $_GET or $_POST etc...

try this

// empty preforms a isset also so you don't need both
if (!empty($_GET['addcode']))
{
// form goes here
}


I assume from some of your coding that you are using an older version of PHP or register globals is enabled. I'd suggest you read up on global variables and how register_globals is bad. (here (http://us2.php.net/register_globals))

infodeamon
November 22nd, 2004, 06:19 AM
1) first the sql query u have written is no correct
please make a note of this :-->$'comments'";

$sql = "INSERT INTO lmg_daily SET
date='$date',
code='$code',
comments=$'comments'";

2) now onwards when ever u use a variable in PHP
first define it.
ex: $submitcode="";

3) just try out this

$addcode="";

if(!empty($_REQUEST[addcode]))
$addcode=$_REQUEST[addcode];

if (isset($addcode)):
u will get what u r expecting....

Thats it ......:_)
Happy Coding