Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

I have an isssue with my if else statement

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

I have an isssue with my if else statement

Postby PhPClueless » Thu Apr 10, 2014 10:29 am

Everytime i try and correct the if satement it just displays 1 option or the other I'm new with php so excuse me if this seems like a stupid question here's my code:
Code: Select all
 $dbuser = 'xxxxx';
 $dbpass = 'xxxxxx'; 
 $dbhost= 'xxxxxx; 
 $db= 'xxxxxxx';   
 //------------------------------------------------------------------------------------//
  $conn = mysql_connect($dbhost,$dbuser,$dbpass) or  die(mysql_error());   //connect to mysql
 mysql_select_db($db,$conn) or die('Could not connect:'. mysql_error());    //to retreive records

 //----------------------------------------------------------------------------------//



  $Car_ID=$_POST['Car_ID'];

  $query = "delete from CARS where id= '$Car_ID'";
  mysql_query($query);

 
  mysql_select_db('xxxxxx');
$retval = mysql_query( $query, $conn );
if($retval )
{
  die('Could not delete data: ' . mysql_error());
 
  }

else
{
  echo "Deleted data successfully\n";

  //a query is issued to display modified records
 $query="SELECT * FROM CARS";
 $result=mysql_query($query);
 $num=mysql_numrows($result);

 mysql_close();

//-----------the word "Database Output" is displayed
 echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="1" cellspacing="1" cellpadding="6">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Car_ID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Car_Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Fuel_Type</font></th>
<th><font face="Arial, Helvetica, sans-serif">Transmission</font></th>
<th><font face="Arial, Helvetica, sans-serif">Engine_Size</font></th>
<th><font face="Arial, Helvetica, sans-serif">Doors</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total</font></th>
<th><font face="Arial, Helvetica, sans-serif">Available</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date_Added</font></th>
</tr>

<?
$i=0;
while ($i < $num)
{ //--------------Result is displayed
  $Car_ID=mysql_result($result,$i,"id");
  $Car_Name=mysql_result($result,$i,"Car_Name");
  $Fuel_Type=mysql_result($result,$i,"Fuel_Type");
  $Transmission=mysql_result($result,$i,"Transmission");
  $Engine_Size=mysql_result($result,$i,"Engine_Size");
  $Doors=mysql_result($result,$i,"Doors");
  $Total=mysql_result($result,$i,"Total");
  $Available=mysql_result($result,$i,"Available");
  $Date_Added=mysql_result($result,$i,"Date_Added");
?>

 <tr>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Car_ID"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Car_Name"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Fuel_Type"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Transmission"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Engine_Size"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Doors"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Total"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Available"; ?></font></td>
  <td><font face="Arial, Helvetica, sans-serif"><? echo "$Date_Added"; ?></font></td>
</tr>
<?
++$i;
}
echo "</table>";
 echo "<br />";
 echo "<br />";
 echo "<td><br><a href='add.html'>Add New Car</a></td><br/>";
 echo "<td><a href='delete.html'>Delete Car</a></td><br/>";
 echo "<td><a href='update.html'>Update Car</a></td><br/>";
 echo "<td><a href='car_menu.html'>Main Menu</a></td>";


I'm trying to make a table print if a record showing updated records and if it is not deleted display message "record couldn't be deleted (error msg)" :(((( any help?
PhPClueless
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Apr 10, 2014 10:14 am

Re: I have an isssue with my if else statement

Postby seandisanti » Thu Apr 10, 2014 4:34 pm

You should look into PDO for database interactions, it's more secure, and performs better, and is not any harder to implement. That said, what is the output you're getting?
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: I have an isssue with my if else statement

Postby PhPClueless » Fri Apr 11, 2014 12:03 am

I'm getting delete successful and displays the table even though I've tested it by trying to delete the same table data twice.,.. :( :-x
PhPClueless
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Apr 10, 2014 10:14 am

Re: I have an isssue with my if else statement

Postby seandisanti » Fri Apr 11, 2014 12:45 am

Look at your logic. specifically this part.

$retval = mysql_query( $query, $conn );
if($retval )
{
die('Could not delete data: ' . mysql_error());

}

mysql_query() for a delete statement returns true upon success. But you're reporting fail for true response, and success otherwise. Hence the false positive. Fix your logic and you should get some helpful output as to why the record isn't being deleted. The documentation for the function is at http://us2.php.net/manual/en/function.mysql-query.php and it starts with the same warning about using deprecated libraries that my first response did.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: I have an isssue with my if else statement

Postby PhPClueless » Fri Apr 11, 2014 3:12 pm

i don't really understand what you're saying...
PhPClueless
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Apr 10, 2014 10:14 am

Re: I have an isssue with my if else statement

Postby seandisanti » Fri Apr 11, 2014 5:57 pm

That was your code i posted, and it only announces failure upon success.

you have 'if($retval)' should be if( ! $retval)
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.