954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

what's wrong in this code?

so, i started at php and i'm doing some exercises from the book, but when i type this code into my "view.php", the output is all wrong...

<?php include("dbconnect.php") ?>
  
  <h2>View my GuestBook!!</h2>

  <?php

  $result=mysql_query("select * from guestbook")
     or die(mysql_error());
  while ($row=mysql_fetch_array($result))
  {
     echo "<b>Name:</b>";
     echo $row["name"];
     echo "\n";
     echo "<b>Location:</b>";
     echo $row["location"];
     echo "\n";
     echo "<b>Email:</b>";
     echo $row["email"];
     echo "\n";
     echo "<b>URL:</b>";
     echo $row["url"];
     echo "\n";
     echo "<b>Comments:</b>";
     echo $row["comments"];
     echo "\n";
     echo "\n";
     echo "\n";
  }
  mysql_free_result($reslut);
  ?>


the output is like this:View my GuestBook!!

Name:"; echo $row["name"]; echo "
\n"; echo "Location:"; echo $row["location"]; echo "
\n"; echo "Email:"; echo $row["email"]; echo "
\n"; echo "URL:"; echo $row["url"]; echo "
\n"; echo "Comments:"; echo $row["comments"]; echo "
\n"; echo "
\n"; echo "
\n"; } mysql_free_result($reslut); ?>

Sign my GuestBook

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

I think your server doesn't recognize php file, so
it doesn't interpret it. Do you install and configure
correctly PHP ?


- Mitko Kostov

MitkOK
Junior Poster
142 posts since Jul 2007
Reputation Points: 59
Solved Threads: 12
 

i installed XAMPP... and i already made another php file that worked correctly, which was the one that writes the information this one is supposed to read...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

here are all my codes:

dbconnect.php

<?php
mysql_connect("localhost","nobody","ydobon")
   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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <h2>Sign my guestbook!</h2>
  <form method="post" action="create_entry.php">
     
     <b>Name:</b>
     <input type="text" size="40" name="name">
     
     <b>Location:</b>
     <input type="text" size="40" name="location">
     
     <b>Email:</b>
     <input type="text" size="40" name="email">
     
     <b>Home Page URL:</b>
     <input type="text" size="40" name="url">
     
     <b>Comments:</b>
     <textarea name="comments" cols="40" rows="4"
     wrap="virtualv"></textarea>
     

     <input type="submit" name="submit" value="Sign!">
     <input type="reset" name="reset" value="Start Over">

     </form>
 </BODY>
</HTML>


create_entry.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php
  include("dbconnect.php");

  if ($_REQUEST["submit"]=="Sign!")
  {
     $query="insert into guestbook(name,location,email,url,comments) values ('"
            .$_REQUEST["name"]
            ."', '"
            .$_REQUEST["location"]
            ."', '"
            .$_REQUEST["email"]
            ."', '"
            .$_REQUEST["url"]
            ."', '"
            .$_REQUEST["comments"]
            ."') "
     ;
     mysql_query($query);
 ?>
 <h2>Thanks!!</h2>
 <h2><a href="view.php">View my GuestBook!!</a></h2>
 <?php
 }
 else
 {
    include("sign.php");
 }
 ?>
 </BODY>
</HTML>


view.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php include("dbconnect.php") ?>
  
  <h2>View my GuestBook!!</h2>

  <?php

  $result = mysql_query("select * from guestbook")
     or die(mysql_error());
  while ($row = mysql_fetch_array($result))
  {
     echo "Name:";
     echo $row["name"];
     echo "\n";
     echo "<b>Location:</b>";
     echo $row["location"];
     echo "\n";
     echo "<b>Email:</b>";
     echo $row["email"];
     echo "\n";
     echo "<b>URL:</b>";
     echo $row["url"];
     echo "\n";
     echo "<b>Comments:</b>";
     echo $row["comments"];
     echo "\n";
     echo "\n";
     echo "\n";
  }
  mysql_free_result($result);
  ?>

  <h2><a href="sign.php">Sign my GuestBook</a></h2>

 </BODY>
</HTML>


everything works just fine until when it gets toview.php... dunno what the problem is...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

I would suspect this lack of a semi-colon might be causing your problem

<?php include("dbconnect.php") ?>


though I'm not sure why you aren't getting an immediate script error from it.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

according to what i know, it shouldn't affect... since it is a single command...

still... tried it, and no response... the same exact results...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

according to what i know, it shouldn't affect... since it is a single command...

still... tried it, and no response... the same exact results...

Yes, I wondered about it and a couple quick tests here came up the same. The semi-colon did not matter. That was the only thing that stood out that might have caused it to switch parsing modes. I don't really see anything else yet that would be problematic.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

did you try it? i mean, the whole 'combo'?

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 
did you try it? i mean, the whole 'combo'?


No, as that would entail creating db tables as well :)

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

true... hehe... i don't think there's a problem with my php setup, since two out of three pages were working just fine...

maybe it's just something unnoticeable... or the example is not well written... i'll try some more and see if it persists...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

It wouldn't hurt to move the include down into the other block before the query and add the semi-colon. Something is definitely kicking it into HTML parsing mode there.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

still nothing... though the problem is @ line 22... after the first echo, since it prints "Name:" correctly, and then the rest of the code kicks in...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

Try it like this,

<h2>View my GuestBook!!</h2>
<?php
require_once("dbconnect.php");
$exe=mysql_query("select * from guestbook")
  or die("SQL Error: ".mysql_error());
while ($r=mysql_fetch_array($exe))
{
   echo "<b>Name: {$r['name']}\nLocation: {$r['location']}\nEmail: {$r['email']}\nURL: {$r['url']}\nComments: {$r['comments']}\n</b>";
}
?>

If that doesn't work, open up a MySQL console and do

use guestbook;
select * from guestbook;

If there is messed up HTML, then theres your problem.

hacker9801
Junior Poster
130 posts since Aug 2007
Reputation Points: 59
Solved Threads: 16
 

output:
View my GuestBook!!

Name: {$r['name']}
\nLocation: {$r['location']}
\nEmail: {$r['email']}
\nURL: {$r['url']}
\nComments: {$r['comments']}
\n"; } ?>
Sign my GuestBook

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

Are you sure you don't have a closing php tag( ?> ) somewhere ?
Type phpinfo(); at the line where it crashes ,to see if the interpreter works at that point( although I'm pretty sure it won't)

Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
 

it stops working right

echo "<b>Name:|<--here-->|</b>|<--or here-->|"
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

it stops working right

echo "<b>Name:|<--here-->|</b>|<--or here-->|"


Yes, that part is clear. Have you checked what is coming from your query for anything like tags that could be interfering as a few people above have mentioned? The code itself looks okay.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

I know your problem.
That is because in your mysql database, you only have a db which name is guestbook.
But you don't have the table which name (according your php code) should also be guest book.
So i think if you create table guestbook("email" varchar(150),"......);
Then you will be o.k.

mikewzp
Newbie Poster
4 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

there is a table called 'guestbook', though, you're right... it doesn't seem to be called in view.php since there is just select * from guestbook unless this 'guestbook' refers to the table... since the db and the table are called the same...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 
there is a table called 'guestbook', though, you're right... it doesn't seem to be called in view.php since there is just select * from guestbook unless this 'guestbook' refers to the table... since the db and the table are called the same...


The 'guestbook' in that query is the table name. If you do have a table called guestbook then all is fine. Either way, that would not produce the behavior you are getting.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You