-1

Ok, so I basically have a form where users can fill out information, on form submission the information gets loaded into my SQL database.

There is an "administrator" page that displays all users that are in my User Table in my DB.

All is well and good that all works perfectly but my problem is that I made the userId's clickable so that when I click on the userId that specific entry will be loaded from the db.

I can't get the userId that I selected printed on the screen.

Here's my Admin page:

<html>
<head>
</head>

<body>

<?php
//Step 1: Set the connection object equal to our database
$conn = mysql_connect("localhost", "root", "") or die (mysql_error());

//Step 2: Set the databse you want to use
mysql_select_db("assignment_3", $conn); 

//Step 3: Write sql that we want to run against the database
$sql = "select id, firstName, lastName, emailAddress from usertable";

//Step 4: Run the sql statement against the database connection object we created $conn

$result = mysql_query($sql, $conn) or die (mysql_error());

//STEP 5: Process the result set $result

print "<table border='1'>";

print "<tr>";

print "<td>id</td>";

print "<td>firstName</td>";

print "<td>lastName</td>";

print "<td>emailAddress</td>";

while ($row = mysql_fetch_assoc($result)) { // this is getting each row that was returned
$clickId = $row['id'];
print "<tr>";   

print "<td><a href ='showUser.php?userId=$clickId'> $clickId </a></td>";
print "<td>$row[firstName]</td>";
print "<td>$row[lastName]</td>";
print "<td>$row[emailAddress]</td>";

}
print "</table>";
?>



</body>

</html>

If you run this page all the users in the db are loaded and displayed in a table and all of their userId's become clickable links when clicked take you to the showUser.php page which displays the information for the user that was selected.

Here is the code for the showUser.php page. I need to grab the userId with the GET method from the URL and then query the DB against that ID so show the user information.

I'm really stuck and need some help.

<html>
<head>
</head>

<body>

<?php

$id = filter_input(INPUT_GET, "userId"); 
//Make db connection
$conn=mysql_connect("localhost","root","")or die(mysql_error());

//Step 2: select your db to use
mysql_select_db("assignment_3",$conn);

//Step 3: Write the sql that we want to run against the database
$sql = mysql_query("select id, firstName, lastName, emailAddress, phoneNumber, underGradSchoolId, gradSchoolId, securityQuestionId from usertable where id='$id'");

$result = mysql_query($sql,$conn)or die(mysql_error());

print "<table border='1'>";

print "<tr>";

print "<td>id</td>";

print "<td>firstName</td>";

print "<td>lastName</td>";

print "<td>emailAddress</td>";

print "<tr>";   

print "<td>id</td>";
print "<td>firstName</td>";
print "<td>lastName</td>";
print "<td>emailAddress</td>";
print "<td>phoneNumber</td>";
print "<td>underGradSchoolId</td>";
print "<td>gradSchoolId</td>";
print "<td>securityQuestionId</td>";

print "</table>";


?>



</body>

</html>
kero
  • 10,647
  • 5
  • 41
  • 51
user2880722
  • 37
  • 1
  • 2
  • 7

2 Answers2

2

You are using mysql_query twice:

$sql = mysql_query("select id, ...");
$result = mysql_query($sql,$conn)or die(mysql_error());

Which should be (renamed so it is like the first code you posted)

$result = mysql_query("select id, ...") or die(mysql_error());
$row = mysql_fetch_assoc($result);

Now you can print the values like

print "<td>".$row['id']."</td>";

Also keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
0

First verify that your getting the value you expect with $id. After step 3, print $sql and make sure it ends with "where id = '{selected id}'"