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

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.