Hi I have a table with info that I want to read and display.
I can read it and display but I cant seem to format it can anyone tell me whats wrong.

<html>
<head>
<title>comment</title>
</head>


<body>
<form action="postcomment.php" method="post">
name: <input type="text" name="name" ><br>
comment: <textarea name= "comment" type = "text"></textarea><br>
<input type="submit" name="submit value="submit">

</form>
</body>
</html>
<?php 
$server = "localhost";	// server to connect to.
$database = "";	// the name of the database.
$db_user = "";	// mysql username to access the database with.
$db_pass = "";	// mysql password to access the database with.
$table = "comment";

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['name']."', '".$_POST['comment']."')")
or die("Could not insert data because ".mysql_error());

// print a success message
echo "Your comment was posted!<br>"; 
?>
<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="comment"; // Table name 


// connect to the mysql server
$link = mysql_connect($host, $username, $password)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
$db_found = mysql_select_db($db_name)
or die ("Could not select database because ".mysql_error());


$result=mysql_query("select * from $tbl_name");
while($row=mysql_fetch_assoc($result)){
echo "ID: $row['id'] <br/>";
echo "name :$row['name'] <br/>";
echo "comment: $row['comment'] <br/>";
}

}

?>
CREATE TABLE `comment` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL default '',
`comment` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

i get this error
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'

please can someone have a look and tell me how i can format the output thank you.

Recommended Answers

All 2 Replies

In your third block of code you have:

$result=mysql_query("select * from $tbl_name");
while($row=mysql_fetch_assoc($result)){
echo "ID: $row <br/>";
echo "name: $row <br/>";
echo "comment: $row <br/>";
}

And I would do it like:

$result=mysql_query("select * from $tbl_name");
while($row=mysql_fetch_assoc($result)){
echo "ID: ".$row['id']."<br>name: ".$row['name']."<br>comment: ".$row['comment']."<br><br>";
}

It will display the data without error. The double <br> at the end gives an extra line between the blocks. When your text after an echo is between double qoutes (") the field names should be between singles ones ('). You can switch this like:

echo 'something: '.$row["field"].'<br>';

Bottom line is that your reference to variables is not included inside the echo (have seen code where it works, but I never do it).

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.