Hi there!
I've been working from a book and the first application that is written in the book appears to have a bug in it! 🙁
The following is the code:
[b]dbconnect.php[/b]
<?php
mysql_connect("localhost","root","") or
die ("Could not connect to database.");
mysql_select_db ("guestbook") or
die ("Could not select database.");
?>
------> Sign.php (html written)
<html>
<head><title>Guestbook :: Sign my guestbook!</title></head>
<body>
<h2>Sign my Guest Book!</h2>
<form method=post action="create_entry.php">
<b>Name:</b>
<input type=text size=40 name=name>
<br>
<b>Location:</b>
<input type=text size=40 name=location>
<br>
<b>Email:
<input type=text size=40 name=email>
<br>
<b>Home Page URL:</b>
<input type=text size=40 name=url>
<br>
<b>Comments:</b>
<textarea name=comments cols=40 rows=4 wrap=virtual></textarea>
<br>
<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over!">
</form>
</body>
</html>
[b]create_entry.php[/b]
<?php
include("dbconnect.php");
if ($submit == "Sign!")
{
$query = "insert into questbook
(name,location,email,url,comments) values
('$name','$location','$email','$url','$comments')"
;
mysql_query($query) or
die (mysql_error());
?>
<h2>Thanks!</h2>
<h2><a href="view.php">View my guestbook!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>
[b]View.php[/b]
<?php include("dbconnect.php"); ?>
<h2>View My Guest Book!</h2>
<?php
$result = mysql_query("select * from guestbook") or
die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>Name:</b>;
echo $row["name"];
echo "<br>\n";
echo "<b>Location:</b>";
echo $row["location"];
echo "<br>\n";
echo "<b>Email:</b>";
echo $row["email"];
echo "<br>\n";
echo "<b>URL:</b>";
echo $row["url"];
echo "<br>\n";
echo "<b>Comments:</b>";
echo $row["comments"];
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
}
mysql_free_result($result);
?>
<h2><a href="sign.php">Sign My Guest Book!</a></h2>
As you can see, sign.php is the html page that posts all the information in the form to create_entry.php... once it gets to create_entry.php, supposedly it'll write the information to the database. Now it can connect to the database but the problem is with the following part:
-------> Sign.php
<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over!">
</form>
</body>
</html>
[b]create_entry.php[/b]
<?php
include("dbconnect.php");
if ($submit == "Sign!")
The parts highlighted in bold are the problem... the part in sign.php doesn't appear to show itself as submit to create_entry.php as I get an error message when I submit the information which is:
Notice: Undefined variable: submit in C:\Inetpub\wwwroot\PHP\create_entry.php on line 4
Now line 4 is the one that says:
if ($submit == "Sign!")
Any ideas?
Thanks,
Chris