Friends, i am new to the world of php and database programming. I apologize ahead if i fail to communicate well using the right terminologies.
Table 1
Id | name | lastName | age
===============================
1 | Jane | Kot | 20
2 | Ann | Ama | 17
Table 2
Id | lastName | bonus | sex
==============================
1 | kot | 50% | male
2 | Jack | 20% | male
I want to join the two tables where table1.lastName = table2.lastName. In the illustration above, I would be looking forward to having “1 Jane 20 (all from table1) 1 50% male (all from table2) returned. But I would choose to display any of the data, hyper-linking it to display the entire record in another page using the common field (lastName)
Example: Jane, displayed in page1.php, when clicked will display the combined record in page2.php. Jane (now hyper-linked in page1.php) ---------clicked---------> 1 Jane 20 1 50% male (displayed in page2.php) Please help me. I apologize for my poor mode of expressing my problem. Thanks
CODE FOR PAGE1.PHP
$sql = "SELECT * FROM table1, table2 WHERE table1.lastname = table_2.lastname";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
echo '<table border="1">';
echo '<tr>';
echo '<td>'. $row['name'].'<br>'.'</td>';
echo '<td><a href="page2.php? lastName=' . $row['lastName'] . '">VIEW DETAILS</a></td>';
echo'</tr>';
echo'</table>';
}
?>
CODE FOR PAGE2.PHP
$lastName = mysql_real_escape_string($_GET['lastName']);
$sql =mysql_query ('SELECT name, age, bonus, sex FROM table_1 c join table_2 f on c.lastname = f.lastname where c.lastname = $lastname ' )
or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
while ($row) {
// Echo page content
echo $row['name'].'<br>';
echo $row['age'];
echo $row['sex'];
echo $row['bonus'];
}
?>
When i clicked on "View Details" in page1.php, I get this error Unknown column '$lastname' in 'where clause'
Aucun commentaire:
Enregistrer un commentaire