By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,972 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,972 IT Pros & Developers. It's quick & easy.

Hep req. with PHP and MySQL - simple guestbook

P: n/a
I am a beginner in PHP and MySQL, and am working through a book and various
online tutorials on PHP and MySQL and now stuck - installed everything on
"localhost" and it all works fine. My question is as follows.

I have a guestbook type code I'm trying to write, but when the results get
printed to a webpage, it outputs completely nothing as what was entered in
the guestbook database. No errors appear to be generated, and I can't see
if it's the form submission to the database that is producing "null"
results, or not reading from the database properly / writing to HTML
properly. The form's "post" action executes the PHP.

Can anyone point out where I'm going wrong?

Below is the HTML code and the seperate PHP file I'm using.

Thanks for any help

Dariusz

-->> HTML

<FORM METHOD="POST" ACTION="../../db_connect.php">
Your Name (nickname):<BR>
<input type="text" name="gbName" size="20" maxlength="20">
<BR>
Personal Website:<BR>
<input type="text" name="gbURL" size="20" maxlength="80">
<BR>
Your Comment:<BR>
<TEXTAREA name="gbComment" cols="40" rows="5"
wrap="virtual"></textarea><BR>
<BR>
<input type="submit" name="submit" value="Add to the guestbook">
</FORM>
-->> PHP

<?
// Database name
$DatabaseName = "Guestbook";
// Database Table name to look for
$table_to_look_for="entries";

// Connect (log on) to the database
$connection = @mysql_connect("localhost") or die("<B>Could not connect to
MySQL: </B>".mysql_error());

// Test if database exists, if it does, do nothing - if not, create the new
database
mysql_select_db("$DatabaseName") or @mysql_create_db($DatabaseName,
$connection);

// Test to see if the table exists, if it does do nothing - if not, create
it.
$i = 0;
$exists=0;

// get all tables in the database
$result = @mysql_list_tables ("DatabaseName");

// loop through result to look for your table
while ($tbl=@mysql_tablename($result, $i++))
{
if($tbl==$table_to_look_for)
{
$exists=1;
}
else
{
// Add data to create the fields needed in the guestbook
mysql_query("CREATE TABLE entries(
gbID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
gbDate DATETIME NOT NULL,
gbIP VARCHAR(15),
gbURL VARCHAR(80),
gbName VARCHAR(20),
gbComment TEXT NOT NULL)"
)or die("Problem creating database table: ".mysql_error());
}
}
// Enter the data into the guestbook database
$sql = ("INSERT INTO entries(gbID, gbDate, gbIP, gbURL, gbName, gbComment)
VALUES('$_POST[ID]', '$_POST[Date]', '$_POST[IP]', '$_POST[url]',
'$_POST[Name]', '$_POST[Comment]')");

$result = @mysql_query($sql, $connection) or die("<B>Problem entering data
into database: </B>".mysql_error());
// Display database entries
$sql = "SELECT gbid, gbDate, gbIP, gbURL, gbName, gbComment FROM
$table_to_look_for ORDER BY gbid";

$result = @mysql_query($sql, $connection) or die("<B>Problem reading from
database: </B>".mysql_error());

while ($row = mysql_fetch_array($result))
{
$gbID = $row['id'];
$gbDate = $row['Date'];
$gbURL = $row['URL'];
$gbName = $row['Name'];
$gbComment = $row['Comment'];

$display_entry .= "$Name<BR>IP: Logged &nbsp;Posted: $Date<BR>Personal
website address: $URL<BR>Comment: $Comment<BR><BR>";
}

?>
<HTML>
<HTAD>
<TITLE>Test - Guestbook</TITLE>
</HEAD>
<BODY>

<? echo "running"; ?>

<?
echo "$display_entry";
MYSQL_CLOSE();
?>

</BODY>
</HTML>

The following is the result of the guestbook running, same for however many
entries is put in / read out.

""
IP: Logged Posted:
Personal website address:
Comment:
""
Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The problem that jumps out at me is the variable names you're inserting into
the database are missing the "gb" prefix.('$_POST[ID]', '$_POST[Date]',
'$_POST[IP]', '$_POST[url]', '$_POST[Name]', '$_POST[Comment]'). These
should be the same as you named them in your form:

$_POST[bgURL], $_POST[gbName], $_POST[gbComment]

The other variables don't seem to be in the form, so won't be posted and
contain null:

$_POST[ID], $_POST[Date], $_POST[IP]

A place to start anyway...

"Dariusz" <ng@lycaus.plusYOURSHIT.com> wrote in message
news:b9*******************@wards.force9.net...
I am a beginner in PHP and MySQL, and am working through a book and various online tutorials on PHP and MySQL and now stuck - installed everything on
"localhost" and it all works fine. My question is as follows.

I have a guestbook type code I'm trying to write, but when the results get
printed to a webpage, it outputs completely nothing as what was entered in
the guestbook database. No errors appear to be generated, and I can't see
if it's the form submission to the database that is producing "null"
results, or not reading from the database properly / writing to HTML
properly. The form's "post" action executes the PHP.

Can anyone point out where I'm going wrong?

Below is the HTML code and the seperate PHP file I'm using.

Thanks for any help

Dariusz

-->> HTML

<FORM METHOD="POST" ACTION="../../db_connect.php">
Your Name (nickname):<BR>
<input type="text" name="gbName" size="20" maxlength="20">
<BR>
Personal Website:<BR>
<input type="text" name="gbURL" size="20" maxlength="80">
<BR>
Your Comment:<BR>
<TEXTAREA name="gbComment" cols="40" rows="5"
wrap="virtual"></textarea><BR>
<BR>
<input type="submit" name="submit" value="Add to the guestbook">
</FORM>
-->> PHP

<?
// Database name
$DatabaseName = "Guestbook";
// Database Table name to look for
$table_to_look_for="entries";

// Connect (log on) to the database
$connection = @mysql_connect("localhost") or die("<B>Could not connect to
MySQL: </B>".mysql_error());

// Test if database exists, if it does, do nothing - if not, create the new database
mysql_select_db("$DatabaseName") or @mysql_create_db($DatabaseName,
$connection);

// Test to see if the table exists, if it does do nothing - if not, create
it.
$i = 0;
$exists=0;

// get all tables in the database
$result = @mysql_list_tables ("DatabaseName");

// loop through result to look for your table
while ($tbl=@mysql_tablename($result, $i++))
{
if($tbl==$table_to_look_for)
{
$exists=1;
}
else
{
// Add data to create the fields needed in the guestbook
mysql_query("CREATE TABLE entries(
gbID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
gbDate DATETIME NOT NULL,
gbIP VARCHAR(15),
gbURL VARCHAR(80),
gbName VARCHAR(20),
gbComment TEXT NOT NULL)"
)or die("Problem creating database table: ".mysql_error());
}
}
// Enter the data into the guestbook database
$sql = ("INSERT INTO entries(gbID, gbDate, gbIP, gbURL, gbName, gbComment)
VALUES('$_POST[ID]', '$_POST[Date]', '$_POST[IP]', '$_POST[url]',
'$_POST[Name]', '$_POST[Comment]')");

$result = @mysql_query($sql, $connection) or die("<B>Problem entering data
into database: </B>".mysql_error());
// Display database entries
$sql = "SELECT gbid, gbDate, gbIP, gbURL, gbName, gbComment FROM
$table_to_look_for ORDER BY gbid";

$result = @mysql_query($sql, $connection) or die("<B>Problem reading from
database: </B>".mysql_error());

while ($row = mysql_fetch_array($result))
{
$gbID = $row['id'];
$gbDate = $row['Date'];
$gbURL = $row['URL'];
$gbName = $row['Name'];
$gbComment = $row['Comment'];

$display_entry .= "$Name<BR>IP: Logged &nbsp;Posted: $Date<BR>Personal
website address: $URL<BR>Comment: $Comment<BR><BR>";
}

?>
<HTML>
<HTAD>
<TITLE>Test - Guestbook</TITLE>
</HEAD>
<BODY>

<? echo "running"; ?>

<?
echo "$display_entry";
MYSQL_CLOSE();
?>

</BODY>
</HTML>

The following is the result of the guestbook running, same for however many entries is put in / read out.

""
IP: Logged Posted:
Personal website address:
Comment:
""

Jul 16 '05 #2

P: n/a
I noticed that Message-ID: <sU49b.3317$CU3.1706@pd7tw3no> from Phlarmf
contained the following:
The problem that jumps out at me is the variable names you're inserting into
the database are missing the "gb" prefix.('$_POST[ID]', '$_POST[Date]',
'$_POST[IP]', '$_POST[url]', '$_POST[Name]', '$_POST[Comment]'). These
should be the same as you named them in your form:


Well I'm pretty new at this lark but one thing I've learned is to get one
bit working at a time.

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 16 '05 #3

P: n/a
On Sun, 14 Sep 2003 15:25:31 -0500, Dariusz created an award-winning crop
circle <b9*******************@wards.force9.net>, which, when translated
into English, means this:

[ comments below ]
I am a beginner in PHP and MySQL, and am working through a book and various
online tutorials on PHP and MySQL and now stuck - installed everything on
"localhost" and it all works fine. My question is as follows.

I have a guestbook type code I'm trying to write, but when the results get
printed to a webpage, it outputs completely nothing as what was entered in
the guestbook database. No errors appear to be generated, and I can't see
if it's the form submission to the database that is producing "null"
results, or not reading from the database properly / writing to HTML
properly. The form's "post" action executes the PHP.

Can anyone point out where I'm going wrong?

Below is the HTML code and the seperate PHP file I'm using.

Thanks for any help

Dariusz

-->> HTML

<FORM METHOD="POST" ACTION="../../db_connect.php">
Your Name (nickname):<BR>
<input type="text" name="gbName" size="20" maxlength="20">
<BR>
Personal Website:<BR>
<input type="text" name="gbURL" size="20" maxlength="80">
<BR>
Your Comment:<BR>
<TEXTAREA name="gbComment" cols="40" rows="5"
wrap="virtual"></textarea><BR>
<BR>
<input type="submit" name="submit" value="Add to the guestbook">
</FORM>
-->> PHP

<?
Put error_reporting(E_ALL) up here to let PHP catch more
errors for you.
// Database name
$DatabaseName = "Guestbook";
// Database Table name to look for
$table_to_look_for="entries";

// Connect (log on) to the database
$connection = @mysql_connect("localhost") or die("<B>Could not connect to
MySQL: </B>".mysql_error());

// Test if database exists, if it does, do nothing - if not, create the new
database
mysql_select_db("$DatabaseName") or @mysql_create_db($DatabaseName,
$connection);

// Test to see if the table exists, if it does do nothing - if not, create
it.
$i = 0;
$exists=0;

// get all tables in the database
$result = @mysql_list_tables ("DatabaseName");

// loop through result to look for your table
while ($tbl=@mysql_tablename($result, $i++))
{
if($tbl==$table_to_look_for)
{
$exists=1;
}
else
{
// Add data to create the fields needed in the guestbook
mysql_query("CREATE TABLE entries(
gbID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
gbDate DATETIME NOT NULL,
gbIP VARCHAR(15),
gbURL VARCHAR(80),
gbName VARCHAR(20),
gbComment TEXT NOT NULL)"
)or die("Problem creating database table: ".mysql_error());
}
}
// Enter the data into the guestbook database
$sql = ("INSERT INTO entries(gbID, gbDate, gbIP, gbURL, gbName, gbComment)
VALUES('$_POST[ID]', '$_POST[Date]', '$_POST[IP]', '$_POST[url]',
'$_POST[Name]', '$_POST[Comment]')");
Your HTML form names these fields
gbURL, gbName and gbComment, but here you name
them URL, Name, and Comment.

error_reporting(E_ALL) would have caught this for you.

$result = @mysql_query($sql, $connection) or die("<B>Problem entering data
into database: </B>".mysql_error());
// Display database entries
$sql = "SELECT gbid, gbDate, gbIP, gbURL, gbName, gbComment FROM
$table_to_look_for ORDER BY gbid";

$result = @mysql_query($sql, $connection) or die("<B>Problem reading from
database: </B>".mysql_error());

while ($row = mysql_fetch_array($result))
{
$gbID = $row['id'];
$gbDate = $row['Date'];
$gbURL = $row['URL'];
$gbName = $row['Name'];
$gbComment = $row['Comment'];
There is no 'Comment' field in your row, because it was
named 'gbComment' when you did the SELECT query above.

$display_entry .= "$Name<BR>IP: Logged &nbsp;Posted: $Date<BR>Personal
website address: $URL<BR>Comment: $Comment<BR><BR>";
}

First, you write
$gbComment = $row['Comment']
but then you write
$Comment<BR><BR>
?>
<HTML>
<HTAD>
<TITLE>Test - Guestbook</TITLE>
</HEAD>
<BODY>

<? echo "running"; ?>

<?
echo "$display_entry";
MYSQL_CLOSE();
?>

</BODY>
</HTML>

The following is the result of the guestbook running, same for however many
entries is put in / read out.

""
IP: Logged Posted:
Personal website address:
Comment:
""


If I were you, I would keep the pages that create
the database and tables separate from the pages
that use the database and tables. Your pages will
be more modular that way.

Also, I advise re-writing the page from scratch, so
you can ensure that you consistently use variable
names.

For example, when you get $_POST['gbComment'] from
your HTML form, just use $_POST['gbComment'] all
the way through.

When you do the SELECT to check if the guestbook
entry is in the table, just use $row['gbComment']
all the way. There is no need to assign to other
variables such as $Comment that may confuse you.

Jul 16 '05 #4

P: n/a
On Sun, 14 Sep 2003 20:25:31 GMT, Dariusz <ng@lycaus.plusYOURSHIT.com>
wrote:
I am a beginner in PHP and MySQL, and am working through a book and
various online tutorials on PHP and MySQL and now stuck - installed
everything on "localhost" and it all works fine. My question is as
follows.

I have a guestbook type code I'm trying to write, but when the results
get printed to a webpage, it outputs completely nothing as what was
entered in the guestbook database. No errors appear to be generated, and
I can't see if it's the form submission to the database that is producing
"null" results, or not reading from the database properly / writing to
HTML properly. The form's "post" action executes the PHP.
Can anyone point out where I'm going wrong?

Below is the HTML code and the seperate PHP file I'm using.

Thanks for any help

Dariusz

Keep the creation of tables out of the code.
I use phpMyAdim to set up databases and tables.
It is available at phpMyAdmin.sourceforge.net if you are interested.
It is altso a good source for learning php.
Of cource it assumes that you have administrative priveleges of a apache
server.

John
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.