I have a PHP page that spits out the ID # and the Last Name from a table in a database.
I want to be able to click on the ID #, have it go to a new page, and view the rows info for that specfic ID # only.
Here is some code from my print.php page where it lists out the ID # and the Last Name which works great.
ID # = m_id
Last Name = m2_2
$db = new mysqli('localhost', 'username', 'password', 'name');
if($db->connect_errno > 0)
{
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM `test_gina` ORDER BY m_id DESC";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
if($row['m_id'] =="x"){echo " ";}else{ echo '<a href="id.php">'; echo $row['m_id'];}echo '</a>';
if($row['m2_2'] =="x"){echo " ";}else{echo $row['m2_2'];}echo '<br>';
When it goes to the id.php, it shows ALL the rows from the table in the database.
I am not that expierenced in databases, so I know I am doing it wrong, but how do I "connect" the 2 pages to have id.php only show the information for that specfic ID #?
Here is some of my code from my id.php page.
$db = new mysqli('localhost', 'username', 'password', 'name');
if($db->connect_errno > 0)
{
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM `test_gina` ORDER BY m_id DESC";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
if($row['m_id'] =="x"){echo " ";}else{echo '<strong>ID #:</strong>'; echo $row['m_id'];}echo '</br>';
echo "";if($row['m_P'] =="x"){echo " ";}else
{
switch($row['m_P'])
{
case "35310":
echo "<strong>Persons living/Household:</strong> 1";
break;
}
Any help would be appreciated greatly!!!
You should really look into writing "clean" codes. That being said, in your "print.php" replace
echo '<a href="id.php">'; echo $row['m_id'];}echo '</a>'
with
echo '<a href="id.php?id='. $row['m_id']. '">'. $row['m_id']. '</a>'
Notice I'm using concatenation, rather than multiple echo
statements.
Briefly put, the "?" and what follows it in the href
attribute of the <a>
tag is what is called a query string.
Then, in your "id.php" file, you get hold of the query string using the GET
super global, like below:
$id = $_GET['id'];
your query string should then look like this:
$sql = "SELECT * FROM `test_gina` WHERE m_id = $id ORDER BY m_id DESC";