adding records to the database
Posted: Sat Jun 13, 2009 11:11 am
Hello All,
I'm a total newbie and am trying to learn MySQL and PHP. I'm following the book MySQL/PHP Database Appplications by Bulger, Greenspan, & Wall.
I'm using MAMP and created the db and wrote the code as the book says. Everything works ok except(Always an exception
), I can't seem to add any records to the db. I know that I can view the records but I can't find what I'm doing wrong trying add a record.
Below is my code. I'm adding all the pages, but I think the problem is in the create_entry.php page. I feel that I can move onto other things as soon as I can get this solved.
Any help is greatly appreciated. Also If this post is not in the right place I apologize and let me know where to go.
dbconnect.php:
<?php
mysql_connect('localhost','joeuser','resueoj')
or die("<h3>could not connect to MySQL</h3>\n");
mysql_select_db('guestbook')
or die("<h3>could not select database 'guestbook'</h3>\n");
?>
----
sign.php:
<h2>Sign my Guestbook!!!</h2>
<form method="post" action="create_entry.php">
<b> Name:</b>
<input type="text" size="40" name="name">
<br>
<b> Email:</b>
<input type="text" size="40" name="email">
<br>
<b> URL:</b>
<input type="text" size="40" name="url">
<br>
<b> Comments:</b>
<textarea name="comments" cols="40" rows="4" wrap="virtualv"></textarea>
<br>
<input type="submit" name="submit" value="Sign!">
<input type="reset" name="reset" value="Start Over">
</form>
----
Crete_entry.php:
<?php
include ("dbconnect.php");
if($_REQUEST["submit"] == "Sign!")
{
$query = "insert into guestbook
(name, email, url, comments) values('"
.$_REQUEST["name"]
."', '"
.$_REQUEST["email"]
."','"
.$_REQUEST["url"]
."','"
.$_REQUEST["comments"]
."')"
;
msql_query($query);
?>
<h2>Thanks!!</h2>
<h2><a href="view.php">View my Guest Book!!!</a></h2>
<?php
}
else
{
include ("sign.php");
}
?>
----
view.php:
<?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>'
, $row['name']
, "<br>\n"
, '<b>Email:</b>'
, $row['email']
, "<br>\n"
, '<b>URL:</b>'
, $row['url']
, "<br>\n"
, '<b>Comments:</b>'
, $row['comments']
, "<br>\n"
, "<br>\n"
, "<br>\n";
}
mysql_free_result($result);
?>
<h2><a href="sign.php">Sign My Guest Book!!</a></h2>
I'm a total newbie and am trying to learn MySQL and PHP. I'm following the book MySQL/PHP Database Appplications by Bulger, Greenspan, & Wall.
I'm using MAMP and created the db and wrote the code as the book says. Everything works ok except(Always an exception
Below is my code. I'm adding all the pages, but I think the problem is in the create_entry.php page. I feel that I can move onto other things as soon as I can get this solved.
Any help is greatly appreciated. Also If this post is not in the right place I apologize and let me know where to go.
dbconnect.php:
<?php
mysql_connect('localhost','joeuser','resueoj')
or die("<h3>could not connect to MySQL</h3>\n");
mysql_select_db('guestbook')
or die("<h3>could not select database 'guestbook'</h3>\n");
?>
----
sign.php:
<h2>Sign my Guestbook!!!</h2>
<form method="post" action="create_entry.php">
<b> Name:</b>
<input type="text" size="40" name="name">
<br>
<b> Email:</b>
<input type="text" size="40" name="email">
<br>
<b> URL:</b>
<input type="text" size="40" name="url">
<br>
<b> Comments:</b>
<textarea name="comments" cols="40" rows="4" wrap="virtualv"></textarea>
<br>
<input type="submit" name="submit" value="Sign!">
<input type="reset" name="reset" value="Start Over">
</form>
----
Crete_entry.php:
<?php
include ("dbconnect.php");
if($_REQUEST["submit"] == "Sign!")
{
$query = "insert into guestbook
(name, email, url, comments) values('"
.$_REQUEST["name"]
."', '"
.$_REQUEST["email"]
."','"
.$_REQUEST["url"]
."','"
.$_REQUEST["comments"]
."')"
;
msql_query($query);
?>
<h2>Thanks!!</h2>
<h2><a href="view.php">View my Guest Book!!!</a></h2>
<?php
}
else
{
include ("sign.php");
}
?>
----
view.php:
<?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>'
, $row['name']
, "<br>\n"
, '<b>Email:</b>'
, $row['email']
, "<br>\n"
, '<b>URL:</b>'
, $row['url']
, "<br>\n"
, '<b>Comments:</b>'
, $row['comments']
, "<br>\n"
, "<br>\n"
, "<br>\n";
}
mysql_free_result($result);
?>
<h2><a href="sign.php">Sign My Guest Book!!</a></h2>