Selecting MySQL database row based on form input

We recently upgraded from PHP4 to PHP5 and the below script that was working perfectly in 4 has completely stopped working and I can’t figure out why for the life of me. I’m not an experienced PHP programmer–I’ve done some forms, but this is the first time I’ve used a database.

What needs to (and was) happen: A user enters their username in the form and gets a readout of their participation so far that month. The form stores the variable ‘user,’ which then tells the database what information to echo.

The problem(s): I know that it’s storing the variable ‘user’ because it echoes it back properly, but the database is no longer allowing me to select the row based on that variable. I know it’s not that I can’t connect to the database because if instead of ‘$user’ I change the code to a username I know is in there, I get the proper readout. This all started as soon as I transferred over to PHP5–before that, no problems at all.

The database information is all correct, I just took it out for privacy’s sake.

[php]


<?php if (isset($_POST['user'])) { $_session['user'] = $_POST['user']; } ?>

You entered your username as: <? echo $_session['user'];?>. If this is not correct or you do not see your information below, please re-enter your username and click Submit again.

<?php $con = mysql_connect("database","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT * FROM March WHERE Username='$user'") or die ('Error: '.mysql_error ()); while($row = mysql_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Username: " . $row['Username'] . "
Mar. 2 Discussion: " . $row['Mar2Q'] . "
Mar. 2 Poll: " . $row['Mar2P'] . "
Mar. 9 Discussion: " . $row['Mar9Q'] . "
Mar. 9 Poll: " . $row['Mar9P'] . "
Mar. 16 Discussion: " . $row['Mar16Q'] . "
Mar. 16 Poll: " . $row['Mar16P'] . "
March Participation To-Date: " . $row['Participation'] . "
"; mysql_close($con); ?> [/php]

ANY help would be greatly appreciated! I have a couple hundred people using this on a regular basis and they’re all starting to ask why it’s all of a sudden not working.

change $user in the WHERE clause to:
‘{$_session[‘user’]}’

or add this line:
$user = $_session[‘user’];
(underneath this one: $_session[‘user’] = $_POST[‘user’]; )

Hope that helps

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service