0

I have some basic code which selects data from a database and echo's it into a HTML table and also add's a link to the echo'd out data. This all works fine. What I want to do is add another piece of data from my database (product_id) to the URL. Here is the code:

<?php
$con=mysqli_connect("localhost","root","","db_test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT * FROM tbl_products");

echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><a href='product.php?id='>" . $row['product_name'] . "</a></td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

I want to add product_id from my database at the end of

<a href='product.php?id='

so the product_id becomes the ID of the page. How would I do this? I have experimented but it has resulted in numerous errors. I am sure this is a simple syntax thing but it' s bugging me.

2 Answers 2

1

You are closing the tag A before placing your data into href.

I think it should work:

echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><a href=\"product.php?id=". $row['product_id'] ."\">" . $row['product_name'] . "</a></td>";
  echo "</tr>";
  }
echo "</table>";

Answer back if it doesn't work.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you mean product_id instead of one of the product_name's? That works. Cheers.
Sure, I copied the whole string and didn't change the array position.
0

The problem is you should put the '> after " . $row['product_name'] . " so that the line would read:

 echo "<td><a href='product.php?id=" . $row['product_name'] . "'>" . $row['product_name'] . "</a></td>";

1 Comment

Everything I have is working fine. What I want to do is add another thing to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.