...

View Full Version : Resolved Multiple return() args?



hinch
02-05-2010, 12:28 PM
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)


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");
}

}

hinch
02-05-2010, 12:36 PM
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:


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(),$useri d)";
$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;
}

}

abduraooft
02-05-2010, 12:42 PM
} else {
return(false,"Company Already Exists");
}
You could define your own logic to handle the return values, like

$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
$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()

hinch
02-05-2010, 12:51 PM
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 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum