carter90 Posted April 24, 2013 Share Posted April 24, 2013 I have this code below which is linked to my mysql database. For some reason it is only showing the price for the if part of the statement? <html> <body> <?php $mysqli = new mysqli("localhost", "root", "", "insuredcars"); if ($_POST['formcar'] == '1' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'"); while($row = mysqli_fetch_assoc($query)) { echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; } echo "</table>"; } elseif ($_POST['formcar'] == '2' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '2'"); echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; echo "</table>"; } elseif ($_POST['formcar'] == '3' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '5'"); echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; echo "</table>"; } elseif ($_POST['formcar'] == '4' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '6'"); echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; echo "</table>"; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/ Share on other sites More sharing options...
Barand Posted April 24, 2013 Share Posted April 24, 2013 Why is that surprising? There are only if statements, not an else in sight. Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/#findComment-1426419 Share on other sites More sharing options...
carter90 Posted April 24, 2013 Author Share Posted April 24, 2013 Sorry I was messing around trying to see if it would work with just if statements forgot I had changed it. Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/#findComment-1426420 Share on other sites More sharing options...
DavidAM Posted April 24, 2013 Share Posted April 24, 2013 You never retrieve the results of the query in any of the ELSEIF blocks. mysqli_query only executes the query, you need to use mysqli_fetch_assoc after that to get the data. And, by the way, if are only selecting one row and only processing one row, you do not need the while loop. You can just do the fetch. Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/#findComment-1426427 Share on other sites More sharing options...
carter90 Posted April 24, 2013 Author Share Posted April 24, 2013 Thanks for that I have added the code in below the elseif statements: I wasn't sure what put instead of while as it comes up with a syntax error if I just take it out. while ($row = mysql_fetch_assoc($query)) Unfortunately now it is coming up with an error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in search.php on line 9 Sorry if I am being an idiot. Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/#findComment-1426432 Share on other sites More sharing options...
DavidAM Posted April 24, 2013 Share Posted April 24, 2013 You are using mysqli in your original code. You can NOT mix mysql calls with that. You must use mysqli_fetch_assoc(). If you are expecting and wanting only ONE row. You just need the $row = mysqli_fetch_assoc($query); without the WHILE () { and, of course, remove the closing brace afterwards. $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'"); $row = mysqli_fetch_assoc($query); #MAD# Added #MAD# while($row = mysqli_fetch_assoc($query)) # Removed #MAD# { # Removed echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; #MAD# } # Removed echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/#findComment-1426442 Share on other sites More sharing options...
carter90 Posted April 24, 2013 Author Share Posted April 24, 2013 You Sir are a legend. So if I was to display two rows I would just stick 'while' back in. Link to comment https://forums.phpfreaks.com/topic/277272-elseif-statement-only-displaying-the-result-of-the-if-part/#findComment-1426446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.