Jump to content

Is there anything wrong in the code below ? I dunno why it is not working.


jd2007

Recommended Posts

$server=mysql_connect("localhost", "root") or
     die("Could not connect: " . mysql_error());
     $connection=mysql_select_db("comment", $server);
     
     $queryc="INSERT INTO comments (name, comment)
              VALUES ($name, $comment)";
     $resultc=mysql_query($query);
     
     $query2="SELECT comments.name, comments.comment
	FROM comments";
     $result2=mysql_query($query2); 
    
     while ($record = mysql_fetch_assoc($result2)) {
                
	while (list($fieldname, $fieldvalue) = each ($record)) {

                
		echo "<B>".$fieldvalue."</B><BR>";
		echo "<BR>"; 

	      }




	}

 

$name and $comment is taken by user input in a form outside this script

"In a form outside this script"

 

Do you mean that a different script makes the $name and $comment variables then you're calling the above script?

 

If this is the case the two variables are lost. You'll need to carry them over with either the URL or by using sessions.

"Newbie, stay. Stay. Bad newbie. That's a very bad newbie."

 

In your code - you need to make sure to not assume that variables are always defined somewhere else.

 

Do something like this (depending on how you plan on sending the variables to this script):

 

$name = (isset($_GET["name"])) ? $_GET["name"] : NULL;
$comment = (isset($_GET["comment"])) ? $_GET["comment"] : NULL;

$server=mysql_connect("localhost", "root") or die("Could not connect: " . mysql_error());
$connection=mysql_select_db("comment", $server);
$queryc="INSERT INTO comments (name, comment) VALUES ($name, $comment)";
$resultc=mysql_query($query);
$query2="SELECT comments.name, comments.comment	FROM comments";
$result2=mysql_query($query2); 

while ($record = mysql_fetch_assoc($result2)) {
    while (list($fieldname, $fieldvalue) = each ($record)) {
        echo "<B>".$fieldvalue."</B><BR>";
        echo "<BR>";
    }
}

 

Change $_Get to whatever and change the variable names to whatever.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.