473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Hep req. with PHP and MySQL - simple guestbook

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
4 15945
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Wolfgang Lang | last post by:
Hello community! I am successfully using mysql connections and operations in more than one php file. But when I want to use this in an included (required("header.inc.php")) php file but nothing...
2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
10
by: Patrick | last post by:
Hi I am a newbie and was just getting started working on a guestbook tutorial when I got an error message upon running my PHP code: Fatal error: Call to undefined function mysql_connect() in...
7
by: Jim | last post by:
I'm using PHP & MySQL to create a simple guestbook. I've created my table and I'm able to load my information in as usual. I would like it to display the latest entry first though. I set an id...
8
by: frizzle | last post by:
Hi there, I have two short questions: 1st: I have a site with a mysql DB. There are 3 admins. They're stored in a table called "admin". Fields in there are 'id','name','passw'. If i use the...
1
by: Rune Runnestř | last post by:
Hi, I have made a small program that doesn't work quite the way it should. It is a guestbook for the web, where visitors can write back their greetings. The program consists of 3 files: -...
6
by: DigitalRick | last post by:
I have been running CDONTS in my ASPpages to send emails to me sent from my guestbook. It had been working fine untill I upgraded to Server 2003 (I am also running Exchange 2003) all locally. I...
3
by: frizzle | last post by:
Hi there, I was wondering the folllowing: when i insert something into a mySQL DB -in a guestbook for instance- i mostly use mysql_escape_string($_POST['comment'). now i've seen...
1
by: pete | last post by:
I seem unable to get the FormView with the SqlDataSource control in ASP.NET 2.0 beta 2 to insert into my MySQL 4.11 database corectly. When I run the page and click on the Insert link a SQL insert...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.