Newbie Question

A

Anonymous

Guest
Hello, I'm new at this and I really need some help. I created a query that pulls in all the rows from a table called Applications and displays the first few fields from those rows:

<?php
$resultID = mysql_query("SELECT E_LastName, E_FirstName, E_SSNumber, A_SubmitDate FROM Applications where G_Display = '0' order by E_LastName", $LinkID);
print "<font size=+1>";
print "Applicants";
print "<table>";
print "<table border=0><tr><th>Last Name&nbsp&nbsp</th>";
print "<th>First Name&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>";
print "<th>Social Security&nbsp&nbsp&nbsp</th>";
print "<th>Submit Date&nbsp&nbsp&nbsp</th>";
print "</tr>";

while ($row = mysql_fetch_row($resultID))
{
print "<tr>";
foreach($row as $field)
{
if ($field == "0") {
($field = "NO");
}
elseif ($field == "1") {
($field = "YES");
}
elseif ($field == "-1") {
($field = "YES");
}
elseif ($field == "") {
($field = "UNKNOWN");
}
print "<td>$field</td>";


}
print "</tr>";
}
print "</table>";
print "</font>";
?>


What I would like to be able to do is to have those results display as a hyperlink that you can click on that will take you to another page which will then display the whole record (row). I know how to format the data the way I want it to display, just am not sure how to create the connection from the main page to a specific record.

Any help would be great!
Thanks!
 
Hi,

What i'd do is to pass an id (via url) from one to the other page and then run a query in the second page recovering the full information for the item selected:

<a href="target.php?id=<?php echo $row['id'];?>">whatever info for the link</a>

Then in target.php, you should do:

$id = mysql_real_escape_string($_GET['id']);

And then run the query with that id value. mysql_real_escape_string is for security purposes. I would not run the full query if not needed (i.e. in the first page you might only need a name and the id or so).

Hope it helps.
 
Thanks Victor... but I just get a blank page, can you double check the syntax on this?
<a href="target.php?id=<?php echo $row['id'];?>">whatever info for the link</a>
 
ok, I overcame my stupidity (briefly) and modified my source page to this:

<td><a href="modules/OSi-applicants/view.php?id=<?php echo $row['id'];?>"><?php echo $field;?></a></td>

So, now my source page (index.php) displays correctly with one very important exception. The link doesn't show the id. All the links end with id=

Your thoughts?
 
hmmm.... good point. You can tell I'm really new...

The problem is I don't have a column called "id", the first column is Primary_Key, would that work instead of id?
 
I tried using Primary_Key and I still don't get the id= at the end.

Here's what I have:

$resultID = mysql_query("SELECT Primary_Key, E_LastName, E_FirstName, E_SSNumber, A_SubmitDate FROM Applications where G_Display = '0' order by E_LastName", $LinkID);
print "<font size=+1>";
print "Applicants";
print "<table>";
print "<table border=0><tr><th>ID&nbsp&nbsp</th>";
print "<th>Last Name&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>";
print "<th>First Name&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>";
print "<th>Social Security&nbsp&nbsp&nbsp</th>";
print "<th>Submit Date&nbsp&nbsp&nbsp</th>";
print "</tr>";

while ($row = mysql_fetch_row($resultID))
{
print "<tr>";
foreach($row as $field)
{
if ($field == "0") {
($field = "NO");
}
elseif ($field == "1") {
($field = "YES");
}
elseif ($field == "-1") {
($field = "YES");
}
elseif ($field == "") {
($field = "UNKNOWN");
}

?>
<td><a href="modules/OSi-applicants/view.php?id=<?php echo $row['Primary_Key'];?>"><?php echo $field;?></a></td>

<?php

}
print "</tr>";
}
print "</table>";
print "</font>";
 
You've used mysql_fetch_row which returns an enumerated array $row[0], $row[1], etc.

But you've used an associative index with $row['Primary_Key']..

So try this:

Code:
<td><a href="modules/OSi-applicants/view.php?id=<?php echo $row[0];?>"><?php echo $field;?></a></td>

Also have a read through the MySQL functions, personally I prefer mysql_fetch_array()

Will.
 
okej..use the loop and no foreach :

while($row = mysql_fetch_assoc($result)){
echo $row['Primary_key']; // this should work
}

please edit the code to suit your needs..
 
Thanks! That worked great for diplaying the link correctly, so... now how do I make the taret page (view.php) work?

Here is what I have, but get a blank page:



$resultID = 'SELECT * FROM Applications WHERE id="'.mysql_real_escape_string($_GET['id']).'" LIMIT 1';


if (!$resultID) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($resultID);
print "<font size=-1>";
echo $row[2]; //E_FirstName
print "&nbsp";
echo $row[3]; //E_Initial
print ".&nbsp";
echo $row[1]; //E_LastName
print "<br>";
echo $row[66]; //E_SSNumber
print "<br><br>";
echo $row[4]; //E_Address1
print "&nbsp&nbsp";
echo $row[5]; //E_Address2
print "<br>";
echo $row[6]; //E_AddressCity
print ",&nbsp";
echo $row[7]; //E_AddressState
print "&nbsp";
echo $row[8]; //E_AddressZip
print "<br>";
echo $row[9]; //E_Telephone1
print "<br>";
echo $row[10]; //E_Telephone2
print "<br>";
echo $row[11]; //E_EMail
print "<br><br>";
 
ofcource you do!

you do not run the query ;) your resultID is holding the query string but you forgot to use mysql_query ()

hehe.. :) please be more carefull :D
 
Oops!

ok, how about this?

$id = mysql_real_escape_string($_GET['id']);
$resultID = mysql_query("SELECT * FROM Applications WHERE id="'.mysql_real_escape_string($_GET['id']).'" LIMIT 1");

I still get a blank page. Should I be using Primary_Key or Row0 instead of id?

Can you please edit my code directly? I am easily confused. (obviously)
Thanks!
 
This one should do it

Code:
<?php
$sql = 'SELECT * FROM Applications WHERE id="'.mysql_real_escape_string($_GET['id']).'" LIMIT 1';
$result = mysql_query($sql) or  die('Could not run query: ' . mysql_error());

$row = mysql_fetch_assoc($result);
print "<font size=-1>";
echo $row['E_FirstName']; //E_FirstName
print "&nbsp";
echo $row['E_Initial']; //E_Initial
print ".&nbsp";
echo $row['E_LastName']; //E_LastName
print "<br>";
echo $row['E_SSNumber']; //E_SSNumber
print "<br><br>";
echo $row['E_Address1']; //E_Address1
print "&nbsp&nbsp";
echo $row['E_Address2']; //E_Address2
print "<br>";
echo $row['E_AddressCity']; //E_AddressCity
print ",&nbsp";
echo $row['E_AddressState']; //E_AddressState
print "&nbsp";
echo $row['E_AddressZip']; //E_AddressZip
print "<br>";
echo $row['E_Telephone1']; //E_Telephone1
print "<br>";
echo $row['E_Telephone2']; //E_Telephone2
print "<br>";
echo $row['E_EMail']; //E_EMail
print "<br><br>";
?>
 
Hi,

What i'd write is:

$id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM Applications WHERE id = '$id' LIMIT 1";
$result =mysql_query($sql, $db);

$row = mysql_fetch_assoc($result);

echo $row['E_FirstName']; and so on.

I just don't like double-quoting and mysql_fetch_row (i find it clearer this way)... If you still do not get anything, try and do echo $sql to see what you are really sending.

Regards.
 
ok, I tried it both ways you guys suggested and got the same error:
Application not found, which is from this:

if (!$resultID) {
echo 'Could not find application';
exit;
}

So, it's not finding that id or primary key.
I'm sending it:
https://inside2.ositranscription.com/modules/OSi-applicants/view.php?id=6358

and there is definately a row with a Primary_Key of 6358.

ideas?
 
Okej... i cant keep a track of your modifications... if you used the whole code ive posted you wouldäve get an error with some debug info as i understand
 
I figured some stuff out (result vs. resultID, duh)

Here's what I have for Alexei's version:

$sql = 'SELECT * FROM Applications WHERE Primary_Key="'.mysql_real_escape_string($_GET['id']).'" LIMIT 1';
$result = mysql_query($sql) or die('Could not run query: ' . mysql_error());
$row = mysql_fetch_assoc($result);

if (!$result) {
echo 'Could not find application';
exit;
}
$row = mysql_fetch_row($result);
print "<font size=-1>";
echo $row['E_FirstName']; //E_FirstName
print "&nbsp";



I now get the fields diplayed with no data in them... that's better than could not find application!
 
Back
Top