Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    Multiple return() args?

    I have a simple function that returns true if the insert was successful and false if the insert failed.
    However I want to add an extra level to the function in the form of a check to see if a record already exists.

    Is it possible to do something like this (see code below)

    PHP Code:
    function addcompany($companyname,$maintel,$address,$mainfax,$postcode,$website,$status,$source) {
        
    $companyname mysql_real_escape_string($companyname);
        
    $maintel mysql_real_escape_string($maintel);
        
    $address mysql_real_escape_string(fixtextarea($address));
        
    $mainfax mysql_real_escape_string($mainfax);
        
    $postcode mysql_real_escape_string($postcode);
        
    $website mysql_real_escape_string($website);
        
    $status mysql_real_escape_string($status);
        
    $source mysql_real_escape_string($source);
        
    $userid $sessionArr[0];
        
        
    $checksql "SELECT Companyname FROM `tblcompanies` WHERE CompanyName='$companyname'";
        
    $chkres mysql_query($checksql);
        
    $chknum mysql_num_rows($chkres);
        
        if (
    $chknum<1) {    
            
    $insertsql "INSERT INTO `tblcompanies` (`CompanyName`,`Address`,`PostCode`,`MainPhone`,`MainFax`,`Website`,`cStatus`,`cSource`,`DateUpdated`,`CompanyOwner`) VALUES ('$companyname','$postcode','$maintel','$mainfax','$website',$status,$source,NOW(),$userid)";
            
    $result mysql_query($insertsql) or die('Invalid query: ' mysql_error() . '<br />'$insertsql);
            if (
    $result) {
                return 
    true;
            } else {
                return 
    false;
            }
        } else {
            return(
    false,"Company Already Exists");
        }


    Last edited by hinch; 02-05-2010 at 12:36 PM.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    ignore me I forgot about global scope and the ability to set variable values outside of the local function scope.

    fixed using this as the function:

    PHP Code:
    function addcompany($companyname,$maintel,$address,$mainfax,$postcode,$website,$status,$source) {
        
    $companyname mysql_real_escape_string($companyname);
        
    $maintel mysql_real_escape_string($maintel);
        
    $address mysql_real_escape_string(fixtextarea($address));
        
    $mainfax mysql_real_escape_string($mainfax);
        
    $postcode mysql_real_escape_string($postcode);
        
    $website mysql_real_escape_string($website);
        
    $status mysql_real_escape_string($status);
        
    $source mysql_real_escape_string($source);
        global 
    $sessionArr;
        
    $userid $sessionArr[0];
        
        
    $checksql "SELECT Companyname FROM `tblcompanies` WHERE CompanyName='$companyname'";
        
    $chkres mysql_query($checksql) or die('Invalid query: ' mysql_error() . '<br />'$checksql);
        
    $chknum mysql_num_rows($chkres);
        
        if (
    $chknum<1) {    
            
    $insertsql "INSERT INTO `tblcompanies` (`CompanyName`,`Address`,`PostCode`,`MainPhone`,`MainFax`,`Website`,`cStatus`,`cSource`,`DateUpdated`,`CompanyOwner`) VALUES ('$companyname','$address','$postcode','$maintel','$mainfax','$website',$status,$source,NOW(),$userid)";
            
    $result mysql_query($insertsql) or die('Invalid query: ' mysql_error() . '<br />'$insertsql);
            if (
    $result) {
                return 
    true;
            } else {
                return 
    false;
            }
        } else {
            global 
    $err2;
            
    $err2 "Company Already Exists";
            return 
    false;
        }


    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    } else {
    return(false,"Company Already Exists");
    }
    You could define your own logic to handle the return values, like
    PHP Code:
    $result=addcompany(....);
    switch(
    $result){
    case: 
    'Invalid': echo "Invalid query"; break;
    case: 
    'Success': echo "Added a new company"; break;
    case: 
    'Duplicate': echo "Already exists"; break;

    for a function that returns three different strings for those cases.

    btw, your code
    PHP Code:
     $result mysql_query($insertsql) or die('Invalid query: ' mysql_error() . '<br />'$insertsql);
            if (
    $result) {
                return 
    true;
            } else {
                return 
    false;
            } 
    won't return anything or proceed further, when the query fails. Thus, you need to ensure the validity of data, before calling mysql_query()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    Quote Originally Posted by abduraooft View Post
    won't return anything or proceed further, when the query fails. Thus, you need to ensure the validity of data, before calling mysql_query()
    force of habit if the query fails it spits out the die() anyway so function never completes so basically the function always returns true unless a record exists and this would create a multiple entry

    its a completely un-nessary if statement tbh but it helps me read through the code at a glance
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •