INDY_tech_student Posted April 24, 2009 Share Posted April 24, 2009 Hello all. I am new to the site. I have a project for class that I'm doing and had a question about it. General facts about the project:I have a table called invoices. In this table there is a field called amt_paid. So on the web page I put in a number for the amount that a student has paid for a given semester. On a different page, the user will put in a student ID with a record from the invoice table. After the student ID is put in the user will see a message from telling them the total the student has paid. If there is no record in the database in the table without that certain student id, there will be a message saying that there is no record in the database with the record. Well here is the code that I have. Please give me some insight. <? //make the database connection $connection = mysql_connect("localhost", "prophett01", "ken01"); mysql_select_db("klprophett01", $connection); //create a query $rate_sql = "select sum(amt_paid) as total_paid from invoices where student_id ='$_POST[student_id]' "; $rate_res = mysql_query($rate_sql, $connection); $rate_row = mysql_fetch_assoc($rate_res); if ($rate_row[student_id] != 0) { $student_sql = "select student_id, student_name, hall_name from invoices where student_id = '$_POST[student_id]'"; $student_res = mysql_query($student_sql); $student_row = mysql_fetch_assoc($student_res); echo "The total amount of money paid by <strong>$student_row[student_name]</strong> living in hall <strong>$student_row[hall_name]</strong> is <strong>$$rate_row[total_paid].00</strong>"; } else { echo "There are no records in the database matching the student id number you put in the text box. <a href='rate_search.html'> Please Search Again</a>"; } ?> The problem I'm having is that even when I put in a student ID from the invoice table I still get the message saying there are no records of that id number in the student id. Quote Link to comment https://forums.phpfreaks.com/topic/155550-problem-with-if-statement-working-correctly/ Share on other sites More sharing options...
rckehoe Posted April 24, 2009 Share Posted April 24, 2009 I have always had problems with using the $_POST[student_id within quotes... You may want to try this: $rate_sql = "select sum(amt_paid) as total_paid from invoices where student_id ='".$_POST[student_id]."' "; Do this with all of the query statements... Quote Link to comment https://forums.phpfreaks.com/topic/155550-problem-with-if-statement-working-correctly/#findComment-818613 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 since you fetched an associative array, you need a string as a key. thus, you have to encase your key around single quotes. So change the following if ($rate_row[student_id] != 0) { to if ($rate_row['student_id'] != 0) { that might fix your problem. Or i might be terribly wrong. Hope it helps regardless! Quote Link to comment https://forums.phpfreaks.com/topic/155550-problem-with-if-statement-working-correctly/#findComment-818639 Share on other sites More sharing options...
INDY_tech_student Posted April 24, 2009 Author Share Posted April 24, 2009 thanks for the help guys but neither one of those options worked for me. I'm guessing the SQL query isnt running and everything is coming back as being matched. The totaling part was working fine but if the student id a user may have put in wasn't in the table the echo would return the string "The total amount of money paid by living in hall is $.00". So I put the if statement in to run if the value the user put in wasn't in the table to return the string "There are no records in the database matching the student id number you put in the text box. Please Search Again". Quote Link to comment https://forums.phpfreaks.com/topic/155550-problem-with-if-statement-working-correctly/#findComment-818717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.