473,387 Members | 1,834 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,387 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 15944
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.