Jump to content

help on simple guestbook script

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
19 replies to this topic

#1
lucky-girl

lucky-girl

    Newbie

  • Members
  • PipPip
  • 29 posts
hi everyone

as a begginer i'm trying to apply a simple guest book tutorial but a problem

everything was fine before starting with mysql :D

i created 3 files:

guest.php
add.php
view.php

in the add.php file, an error in the query

is it right?

 


$sql= "INSERT INTO $tbl_guest(id, guestname, gender, palce, email, title, comment)VALUES ('$id','$guestname', '$gender', '$place', '$email' ,'$title', '$comment')"; // the error is here 


$result=mysql_query($sql);


if($result){

echo "Successful";

echo "<BR>";

echo "<a href='view.php'>View guestbook</a>"; guestbook page

}


else {

echo "ERROR";

}




and an error on view.php file

 $result=mysql_query($sql);


while ($rows = mysql_fetch_array($result)) // the error is here 


{





$result=mysql_query($sql);


while ($rows = mysql_fetch_array($result)) // the error is here 


{

?>


	<table border="1" width="50%" id="table1" height="145" align="center">

	

		<tr>

			<td rowspan="4">

			<p align="left" dir="ltr">Comment Title:

			<? echo $rows['title']; ?>

		 

		?>

			  </p>

			<p align="left" dir="ltr">Comment: <br>

		<? echo $rows['comment']; ?>

		

		</p>

			<p align="left" dir="ltr"> </p>

			<p align="left" dir="ltr">   </td>

			<td width="165">

			<p align="center">

			<font color="#FF0000" size="5" face="Berlin Sans FB Demi">guest name</font>

			</p>

			<p align="center">

			<? echo $rows['guestname']; ?>

			</td>

		</tr>

		<tr>

			<td width="165" align="left">

			<p dir="ltr"> <font color ="red"> my place: </font></p>

			<? echo $rows['place']; ?>

			</td>

		</tr>

		<tr>

			<td width="165" align="left">

			<p dir="ltr">gender : </p>

			<? echo $rows['gender']; ?>

			</td>

		</tr>

		<tr>

		<td width="165" align="left">

			<p dir="ltr">email: </p>

			

  <? echo $rows['email']; ?>


		</td>

		</tr>

	</table>

	

	<?

}

mysql_close(); 

?>



i think i'm writing the code in a wrong way :p

begginer programmer needs the help :)


#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
In add.php change
$result=mysql_query($sql); 
To
$result=mysql_query($sql) or die(mysql_error());
That will give you more information regarding your error.


As for view.php:
while ($rows = mysql_fetch_array($result)) 
appears to be fine, as does the rest of your code below that. What does the exact error say? What does your query in view.php look like? Try adding the
or die(mysql_error());
after that query too.

#3
penkomitev

penkomitev

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
On first look, I see ' ', with a field of type INT. So change this:
VALUES ('$id') with this VALUES ($id).
When you have value of type INT, you should not use ''. After that, if there are still problems, make what Sidewinder said. Happy new year :).
You can visit something interesting HERE

#4
lucky-girl

lucky-girl

    Newbie

  • Members
  • PipPip
  • 29 posts
thank u Sidewinder & penkomitev for your quick answer:)

i applyed what u said Sidewinder,

everythig ok with add.php file :)

but the error still there with view.php file

it says

  Warning: Supplied argument is not a valid MySQL result resource in c:\apache\htdocs\guest_book\view.php on line 29  



and this is line 29

  while ($rows = mysql_fetch_array($result))   

begginer programmer needs the help :)


#5
lucky-girl

lucky-girl

    Newbie

  • Members
  • PipPip
  • 29 posts

penkomitev said:

On first look, I see ' ', with a field of type INT. So change this:
VALUES ('$id') with this VALUES ($id).
When you have value of type INT, you should not use ''. After that, if there are still problems, make what Sidewinder said. Happy new year :).

hi penkomitev,

when i remove the ' ', its appear an error but when i keep it like that its fine, is it important to remove it from INT type?

begginer programmer needs the help :)


#6
penkomitev

penkomitev

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Well, it is not used with INT. But are you sure your field is INT?(Well it may be silly question, but let me to ask everything) From the error you receive, I understand your query is bad. So make: print $sql and try to execute the query trough phpMyAdmin or the one you are using. Look at the error, phpMyAdmin return and you will understand what the problem is. Remebmer from me:

Warning: Supplied argument is not a valid MySQL result resource in c:apachehtdocsguest_bookview.php on line N
in most of cases means bad query.

Happy new year to all of you, who are GMT+1 or higher. For the others, have a little bit more patience :).
You can visit something interesting HERE

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

lucky-girl said:

thank u Sidewinder & penkomitev for your quick answer:)

i applyed what u said Sidewinder,

everythig ok with add.php file :)

but the error still there with view.php file

it says

  Warning: Supplied argument is not a valid MySQL result resource in c:\apache\htdocs\guest_book\view.php on line 29  



and this is line 29

  while ($rows = mysql_fetch_array($result))   

Just because thats where the error is thrown, doesn't mean thats where the error originates. $result is the return value of a mysql query. Do you have a query? I'm almost certain thats where the error is...

#8
lucky-girl

lucky-girl

    Newbie

  • Members
  • PipPip
  • 29 posts
hi penkomitev , Sidewinder & happy new year :)

this is the sql query

  SELECT * FROM `tbl_guest` WHERE 1 LIMIT 0, 30 

i execute the query trough phpMyAdmin and everything is ok

it show the form data from guest.php file

still the problem with the query ?

begginer programmer needs the help :)


#9
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

lucky-girl said:

still the problem with the query ?

Yes, your WHERE clause makes no sense. A WHERE clause is used as a conditional, meaning you need to use an operator so the SELECT statement can evaluate its 'truth.' Have a look here for more information on the SQL WHERE Clause

#10
penkomitev

penkomitev

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Both with Sidewinder were talking about printing
$sql= "INSERT INTO $tbl_guest(id, guestname, gender, palce, email, title, comment)VALUES ('$id','$guestname', '$gender', '$place', '$email' ,'$title', '$comment')"; // the error is here  
When you execute this one, you will see if it works and what data it returns, and the further idea what to do will come suddenly :).
You can visit something interesting HERE

#11
lucky-girl

lucky-girl

    Newbie

  • Members
  • PipPip
  • 29 posts
ahaa i see

ok but the first step on the guest book is to insert the data form in the database and post it on view.php file

still needs another query to add??

begginer programmer needs the help :)


#12
penkomitev

penkomitev

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Yes, and your query for inserting is right $sql. Try to execute it and see if the data is recorded properly in the database.
You can visit something interesting HERE