Forum Moderators: coopster

Message Too Old, No Replies

Using a variable from a $_POST to run a Query

Something wrong with my variables?

         

freshrod

8:28 pm on May 14, 2006 (gmt 0)

10+ Year Member



I have a form that displays some user info from a query. It looks like this:

php:
--------------------------------------------------------------------------------
function displayAlumni($result) {
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $data)
print "{$data}";
print "\n<input type=\"radio\" name=\"alumni\" value=\"{$row[userId]}\">";
}

$query = "SELECT userId, firstName, lastName, maidenName FROM users ORDER BY lastName ASC";

function showerror() {
die ("Error " . mysql_errno() . " : " . mysql_error());
}

if (!($result = mysql_query ($query, $connection)))
showerror(); // The DB info is in an include().

displayAlumni($result);
?>

--------------------------------------------------------------------------------

They select the user with the radio button and it posts to the next page where I want a query to display the User Information. I tried this:

php:
--------------------------------------------------------------------------------
$userId = $_POST['$row[userId]}']; // I also tried $_POST['userId'] with same result... nothing =(.

function displayAlumInfo($result) {

while ($row = mysql_fetch_array($result)) {
foreach ($row as $data)
print "{$data}";
print "\n";
}
}
$query = "SELECT * FROM users WHERE userId = '$userId'";

function showerror() {
die ("Error " . mysql_errno() . " : " . mysql_error());
}

if (!($result = mysql_query ($query, $connection)))
showerror();

displayAlumInfo($result);
?>

--------------------------------------------------------------------------------

Basically, I get nothing. Any Ideas? Thanx.

p.s. I know this shows a lack of security, but I plan on adding some htmlentities() and str_ireplace() later.

eelixduppy

8:38 pm on May 14, 2006 (gmt 0)



$userid = $_POST['alumni'];

eelix

freshrod

9:34 pm on May 14, 2006 (gmt 0)

10+ Year Member



ummm...

I'm not sure i understand what you are trying to tell me.

Please explain.

eelixduppy

9:42 pm on May 14, 2006 (gmt 0)



I'm sorry i didn't have time to write an elaborate reply at the time. You should change the following:
>>> print "\n<input type=\"radio\" name=\"alumni\" value=\"{$row[userId]}\">";
>>>$userId = $_POST['$row[userId]}']; // I also tried $_POST['userId']

to, respectively:
print "\n<input type=\"radio\" name=\"alumni\" value=\"$row[userId]\">";
$userid = $_POST['alumni'];

The latter is because you set the name of the checkbox to 'alumni' and not 'userId'

eelix

freshrod

3:51 am on May 15, 2006 (gmt 0)

10+ Year Member



eelix

Thanks for the help. What you suggested helped... kind of.

I played around with it and got it to display something, but it's really weird.
Here's what I've got now.

the form:
<?php
function displayAlumni($result) {
print "\n<table>\n<tr>\n" .
"\n\t<th>Alumni UserId</th>" .
"\n\t<th>Alumni First Name</th>" .
"\n\t<th>Alumni Last Name</th>" .
"\n\t<th>Alumni Maiden Name</th>" .
"\n\t<th>Select One</th>" .
"\n</tr>";

while ($row = mysql_fetch_assoc($result)) {
print "\n<tr>";
foreach ($row as $data)
print "\n\t<td><center> {$data} </center></td>";
print "\n\t<td><input type=\"radio\" name=\"alumni\" value=\"{$row[userId]}\"></td>";
print "\n</tr>";
}
print "\n</table>\n";
}

$query = "SELECT userId, firstName, lastName, maidenName FROM users ORDER BY lastName ASC";

function showerror() {
die ("Error " . mysql_errno() . " : " . mysql_error());
}

if (!($result = mysql_query ($query, $connection)))
showerror();

displayAlumni($result);
?>

the display page:
<?php
$alumni = $_POST['alumni'];
function displayAlumInfo($result) {

while ($row = mysql_fetch_array($result)) {
foreach ($row as $data)
print "{$data}";
print "\n";
}
}

$query = "SELECT * FROM users WHERE userId = '$alumni'";

function showerror() {
die ("Error " . mysql_errno() . " : " . mysql_error());
}

if (!($result = mysql_query ($query, $connection)))
showerror();

$result = mysql_query ($query);

displayAlumInfo($result);
?>

The weird thing is that it displays the Alumni Information twice. Like it returnd the data from each column and prints it twice and then goes to the next column.

What the heck did I do now?