Forum Moderators: coopster

Message Too Old, No Replies

Help with Inserting link to a Table in PHP

         

pbmsc

2:37 pm on Apr 3, 2011 (gmt 0)

10+ Year Member



I have been trying to get some details from a database and put them to a table using following PHP code. I want to link the last column to separate page view_details.php but it gives following error message. "Parse error: syntax error, unexpected '<' in /home/pbmsc/public_html/viewproducts.php on line 30"

<?php
include ("connection.inc");

mysql_select_db("pbmsc_db", $con);

$result = mysql_query("SELECT * FROM Product");

echo "<table border='1px solid'>
<tr>
<th>Product ID</th>
<th>Category ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Details</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['pr_id']. "</td>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td>" . $row['pr_name'] . "</td>";
echo "<td>" . $row['pr_des'] . "</td>";
echo "<td>" . $row['pr_price'] . "</td>";
echo "<td>" . <a href=\"view_details.php?productid=$row['pr_id'].\"> Details . "</td>"; (line 30)
echo "</tr>";
}
echo "</table>";

echo "<br/>";

mysql_close($con);
?>

Appreciate any input on this. I tried everything i know, but can't seems to get it working.

Matthew1980

5:46 pm on Apr 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello pbmsc,

Welcome to WebmasterWorld!

Try this:-

while($row = mysql_fetch_array($result))
{
echo "<tr>\n\r";
echo "<td>".$row['pr_id']."</td>\n\r";
echo "<td>".$row['cat_id']."</td>\n\r";
echo "<td>".$row['pr_name']."</td>\n\r";
echo "<td>".$row['pr_des']."</td>\n\r";
echo "<td>".$row['pr_price']."</td>\n\r";
echo "<td><a href=\"view_details.php?productid=\".$row['pr_id']."\"> Details</td>\n\r"; (line 30)
echo "</tr>\n\r";
}
echo "</table>\n\r";

echo "<br/>\n\r";

mysql_close($con);//Don't need this...It's a self closing function - see note further down ;)
?>


Something like that anyway, doing the toothpick syndrome (=\") has always been a dodgy way to code html with php interwoven - have a read up on heredoc [php.net] and other ways of string integration - very useful

Also, the use of mysql_close() [uk2.php.net] as quoted in the manual - "isn't usually necessary.."

But for the actual coding faux pas, this was the cause:-

echo "<td>". <a href=\"view_details.php?productid=$row['pr_id'].\">

Corrected version:-

echo "<td><a href=\"view_details.php?productid=\".$row['pr_id']."\"> Details</td>\n\r";

Only jumping into php so that I can echo the value of 'pr_id' and append it to the '?productid=' section in the query string.. which is what you want :)

Basically you were trying to jump into php too quickly, so the parser threw the error, just wondering though, are you using any error reporting at this stage at all? Highly recommended that you have this instruction at the top of your file:-

<?php
//set error reporting level
error_reporting(E_ALL);

Lastly, I added \n\r to the end of all of those lines so that if you were to view this executed code in the 'page source' you wouldn't have 1 huge line of code there in front of you!

Hope that helps you a bit anyway.

Cheers,
MRb

pbmsc

4:49 pm on Apr 4, 2011 (gmt 0)

10+ Year Member



Hi MRb,

Thanks for your reply. Will try this out.

I'm new to PHP, so I have lot to learn, and I'm not using any error reporting at this stage. Thanks for the note on it. Will read further on that topic.