Returning True or False...not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Returning True or False...not working

Post by Sephirangel »

Hey guys,
Right, i have a stock control system where the user enters all details about a sale and the system calls 4 methods to save the sale, save the items within the sale, and also change the inventory accordingly.
when a sale is successfully entered, the function should return with "true" or "false" so i can give the output accordingly.
Here is the function and the code that calls it:
function:

Code: Select all

 
function insertNewSale($salesArray){
        
        $insertToSales = "INSERT IGNORE INTO sales (Date, Time, BuyerID, PostPacking, Payment_Method, PaypalFee, Add_Info) VALUES ('".$salesArray['todaysDate']."','".$salesArray['saleTime']."','".$salesArray['buyerID']."','".$salesArray['postPack']."','".$salesArray['payMeth']."','".$salesArray['paypal']."','".$salesArray['addInfo']."')";
        
        if(mysql_query($insertToSales)){
            return true;
        }else{
            return false;
        }       
    }
 
Page code for output;

Code: Select all

 
if($_SESSION['pageLoad'] == 1){
            $newSale = insertNewSale($_SESSION);
                    
            if($newSale){                   
                $saleN = getSalesNum($_SESSION);
                $insertSaleItem = insertSaleItem($_SESSION, $saleN);
                changeInv($_SESSION);
                
                echo "<div id='checkSale'>";
                echo "<table border=0>";
                echo "<tr><td align = left>Date: </td><td align = right>".$date."</td></tr>";
                echo "<tr><td align = left>Time: </td><td align = right>".$time."</td></tr>";
                echo "<tr><td align = left>Buyer ID: </td><td align = right>".$buyerID."</td></tr>";
                echo "<tr><td colspan=2 align=center>--------------------------------</td></tr>";
                
                for($j = 1; $j <= $noOfItems; $j++){
                    echo "<tr><td align=left>Item Code ".$j."; </td><td align=right>".$_SESSION["ItemCode".$j]."</td></tr>";
                    echo "<tr><td align=left>Quantity ".$j."; </td><td align=right>" .$_SESSION["Quantity".$j]."</td></tr>";
                    echo "<tr><td align=left>Price ".$j."; </td><td align=right>" .$_SESSION["PricePerItem".$j]."</td></tr>";
                    echo "<tr><td colspan=2 align=center>--------------------------------</td></tr>";
                }
                echo "<tr><td align=left>P&P: </td><td align = right>".$postPack. "</td></tr>";
                echo "<tr><td align=left>Payment Method: </td><td align=right>".$payMeth."</td></tr>";
                echo "<tr><td align=left>PayPal Fee: </td><td align=right>".$paypal. "</td></tr>";
                echo "<tr><td align=left>Additional Info: </td><td align=right>".$addInfo."</td></tr>";
                
                echo "<tr><td colspan='2' align=center><a href='newSale.php'><button name='addAnother'>Add Another</button></a>";
                echo "<a href='mainpage.php'><button name='main'>Mainpage</button></a></td></tr></table>";
                echo "</div>";
            }else{
                echo "An error has occured!";
            }
        }
 
The code just above will output the user input to confirm the sale has been added to the database. the $_SESSION['pageLoad'] variable is there as data was being entered twice due to page refresh issues.

Anyway, no matter what, i dont get ANY of the echo'd code come through. However, the functions are carried out as normal, and the records are added to the database even though the functions are within the same if statement as the echoes

Can anyone give me some insight into this please?
thanks in advance!

Josh
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Returning True or False...not working

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Re: Returning True or False...not working

Post by Sephirangel »

thanks, but where would i use it? would i use it in the function and if mysql_affected_rows > 0 then return true?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Returning True or False...not working

Post by VladSun »

yes
There are 10 types of people in this world, those who understand binary and those who don't
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Re: Returning True or False...not working

Post by Sephirangel »

Its still not giving me the HTML.
This is what it looks like as of now:

Code: Select all

    function insertNewSale($salesArray){
        
        $insertToSales = "INSERT IGNORE INTO sales (Date, Time, BuyerID, PostPacking, Payment_Method, PaypalFee, Add_Info) VALUES ('".$salesArray['todaysDate']."','".$salesArray['saleTime']."','".$salesArray['buyerID']."','".$salesArray['postPack']."','".$salesArray['payMeth']."','".$salesArray['paypal']."','".$salesArray['addInfo']."')";
        
        mysql_query($insertToSales);
            
        if(mysql_affected_rows() > 0){
            return true;
        }else{
            return false;
        }   
    }
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Returning True or False...not working

Post by VladSun »

Try this one and see if there are any error messages:

Code: Select all

function insertNewSale($salesArray)
{
        $insertToSales = "INSERT IGNORE INTO sales (Date, Time, BuyerID, PostPacking, Payment_Method, PaypalFee, Add_Info) VALUES ('".$salesArray['todaysDate']."','".$salesArray['saleTime']."','".$salesArray['buyerID']."','".$salesArray['postPack']."','".$salesArray['payMeth']."','".$salesArray['paypal']."','".$salesArray['addInfo']."')";
        
        if (!mysql_query($insertToSales))
             echo mysql_error();
            
        return mysql_affected_rows() > 0;
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Returning True or False...not working

Post by VladSun »

Why do you use IGNORE?
There are 10 types of people in this world, those who understand binary and those who don't
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Re: Returning True or False...not working

Post by Sephirangel »

Im not sure why im using IGNORE, its just something i add to the queries by habit.
I have changed the code but im still not getting the HTML output i should be getting. Im also not getting an error.

Code: Select all

 
    function insertNewSale($salesArray)
    {
            $insertToSales = "INSERT IGNORE INTO sales (Date, Time, BuyerID, PostPacking, Payment_Method, PaypalFee, Add_Info) VALUES ('".$salesArray['todaysDate']."','".$salesArray['saleTime']."','".$salesArray['buyerID']."','".$salesArray['postPack']."','".$salesArray['payMeth']."','".$salesArray['paypal']."','".$salesArray['addInfo']."')";
           
            if (!mysql_query($insertToSales))
                 echo mysql_error();
               
            return mysql_affected_rows() > 0;
   }
 

Code: Select all

 
if($_SESSION['pageLoad'] == 1){
            $newSale = insertNewSale($_SESSION);
                                
            if($newSale){                   
                $saleN = getSalesNum($_SESSION);
                $insertSaleItem = insertSaleItem($_SESSION, $saleN);
                changeInv($_SESSION);
                
                echo "<div id='checkSale'>";
                echo "<table border=0>";
                echo "<tr><td align = left>Date: </td><td align = right>".$date."</td></tr>";
                echo "<tr><td align = left>Time: </td><td align = right>".$time."</td></tr>";
                echo "<tr><td align = left>Buyer ID: </td><td align = right>".$buyerID."</td></tr>";
                echo "<tr><td colspan=2 align=center>--------------------------------</td></tr>";
                
                for($j = 1; $j <= $noOfItems; $j++){
                    echo "<tr><td align=left>Item Code ".$j."; </td><td align=right>".$_SESSION["ItemCode".$j]."</td></tr>";
                    echo "<tr><td align=left>Quantity ".$j."; </td><td align=right>" .$_SESSION["Quantity".$j]."</td></tr>";
                    echo "<tr><td align=left>Price ".$j."; </td><td align=right>" .$_SESSION["PricePerItem".$j]."</td></tr>";
                    echo "<tr><td colspan=2 align=center>--------------------------------</td></tr>";
                }
                echo "<tr><td align=left>P&P: </td><td align = right>".$postPack. "</td></tr>";
                echo "<tr><td align=left>Payment Method: </td><td align=right>".$payMeth."</td></tr>";
                echo "<tr><td align=left>PayPal Fee: </td><td align=right>".$paypal. "</td></tr>";
                echo "<tr><td align=left>Additional Info: </td><td align=right>".$addInfo."</td></tr>";
                
                echo "<tr><td colspan='2' align=center><a href='newSale.php'><button name='addAnother'>Add Another</button></a>";
                echo "<a href='mainpage.php'><button name='main'>Mainpage</button></a></td></tr></table>";
                echo "</div>";
            }else{
                echo "An error has occured!";
            }
        }
 
Like i said, all 4 of those functions are working, its just the echo's that arnt being executed, so the if($newSale) is returning true.

Im pretty puzzled. thanks for the help so far VladSun
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Re: Returning True or False...not working

Post by Sephirangel »

Oh and the reason why i dont just put the HTML after the PHP code is that for some strange reason on my other machine, sometimes the caes records do not enter at all, a happening that is unheard of on my own machine im running it from. The data of each sale is relatively the same so im wrapping it all together so that if the record is entered, the data will be displayed to the user and if it hasnt been saved, it will not.
Post Reply