Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

nested if statements causing error

hello. I seemed to have got lost with my if statements here and cannot see where to use elseif . I am getting the duplicate response message ok, but if I submit with blank input, it produces NaN error, which I know is JavaScript but I think it is because of the order of the if statements. If someone could help me with this I would be very grateful. Thanks.

if (!empty ($_POST['item']))
{
  $data = split(',',$_POST['item'][0]);
  $duplicates = array();

foreach ($data as $val)
{
if ( $val != "" )
 {
  $sql = "SELECT custref FROM boxes WHERE custref='$val' Union SELECT item FROM act WHERE item='$val'";
  $qry = mysql_query($sql) or die(mysql_error());

  if(mysql_num_rows($qry))
  {
  $duplicates[] = $val;
  }
 }
}

if(count($duplicates))
 {
    $response_array['status'] = 'error';
    $response_array['message'] = 'No duplicate files';
 }
}
//check the name field
elseif(empty($authorised)){

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Name cannot be blank';

//check the service field
}

Open in new window

0
peter_coop
Asked:
peter_coop
  • 3
  • 2
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
you are missing one more } at the end...

elseif(empty($authorised)){

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Name cannot be blank';

//check the service field
}

}
0
 
peter_coopAuthor Commented:
@logudotcom
I have posted the whole code because I only posted a part in my original post. Also, I do not think that is the problem because from the code my brackets are closed? Thanks


//response array with status code and message
$response_array = array();

//validate the post form
if (isset($_POST['item']))
{
  $data = split(',',$_POST['item'][0]);
  $duplicates = array();

foreach ($data as $val)
{
if ( $val != "" )
 {
  $sql = "SELECT custref FROM boxes WHERE custref='$val' Union SELECT item FROM act WHERE item='$val'";
  $qry = mysql_query($sql) or die(mysql_error());

  if(mysql_num_rows($qry))
  {
  $duplicates[] = $val;
  }
 }
}

if(count($duplicates))
 {
    $response_array['status'] = 'error';
    $response_array['message'] = 'No duplicate files';
 }
}
//check the name field
elseif(empty($authorised)){

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Name cannot be blank';

//check the service field
} elseif(empty($service)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a service level';

//check the dept field
} elseif($department=="Choose Department") {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must select a department';

//check the address field
} elseif($address=="Choose Address") {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must select a retrieveal address';

//check the box number field
} elseif(!empty($missing)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a box for retrieveal';

//if no errors
}else {

    //set the response
    $response_array['status'] = 'success';
    $response_array['message'] = 'All items retrieved successfully';
    $response_array['info'] = ' You retrieved a total of: ' . $boxcount . ' boxes';

    foreach($boxnumber as $item) {
    $query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`,  `address`, `user`, `item`, `destroydate`, `date`, `notes`, `new`)
             VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($item).'\', NULL, NOW(), \''.$mobile.'\', \''.$new.'\');';
    mysql_query($query) or die(mysql_errno());
 }
}

//send the response back
echo json_encode($response_array);

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperCommented:
Yes, I saw it later,
0
 
peter_coopAuthor Commented:
I have solved this but another problem has arising so I am willing to award you the points on this question because you responded or if you are willing to help me further |I shall raise the points to 500. Thanks
0
 
peter_coopAuthor Commented:
Thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now