0

I have an SQL table (Children):

ID     |   Name
----------------
I03    | Lucy
I05    | Jade
I06    | Jason

I am trying to create a table in PHP which only displays the name in each row, so the table has one column only. I would like each name to have a hyperlink to this page: http://localhost/Child_Stats.php?(x)

'x' must be the child id.

So, e.g. when clicking on Lucy, the hyperlink is: http://localhost/Child_Stats.php?'I03'.

This is my code so far:

$c = mysql_connect("localhost", "root", "password");
if (!$conn) {
            die("Unable to connect: ". mysql_error());
            }
mysql_select_db("table", $conn);
$query = "SELECT * FROM Children";
$data = mysql_query($query, $conn);

echo "<table border = 1> 
<tr>
<th>Child</th>
</tr>"; 

while($record = mysql_fetch_array($data)){
        echo "<tr>";
        echo "<td><a href='http://localhost/Child_Stats.php?'".$record['ID']."''>".$record['Name']."</a> </td>";
        echo "</tr>";
        }

echo "</table>";
mysql_close($conn); //close connection

My code doesn't work because which ever child I select, in the table, the hyperlink is always 'http://localhost/Child_Stats.php?'. The ID for each specific child isn't appended to it.

2
  • Does $record['Name'] have show data ? Commented Apr 10, 2015 at 11:52
  • Also, please note that mysql is deprecated. you should look into mysqli or PDO Commented Apr 10, 2015 at 11:55

1 Answer 1

3

Look at the href attribute - you've put the closing asterisk just after the "?" in the URL. If you look in your HTML source you'll see the ID is being appended AFTER the asterisk. Remove this asterisk, and also one of the two after the ID:

echo "<td><a href='http://localhost/Child_Stats.php?".$record['ID']."'>".$record['Name']."</a> </td>";

EDIT: As @DocRattie said in the comments, you should change your query string to have a name, such as:

...?childid=".$record["ID"]."...
Sign up to request clarification or add additional context in comments.

1 Comment

This and you should think about changing it to Child_Stats.php?ìd=".$record['ID']."' With that you can get the id on the next page with $id = $_GET['id']

Your Answer

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