Jump to content

Elseif statement only displaying the result of the 'if' part


carter90
Go to solution Solved by DavidAM,

Recommended Posts

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>
Edited by carter90
Link to comment
Share on other sites

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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.