user4490696
user4490696

Reputation:

Variable in PHP link with SQL

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.

Upvotes: 0

Views: 227

Answers (1)

Kallum Tanton
Kallum Tanton

Reputation: 777

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

Upvotes: 3

Related Questions