Solved

nested if statements causing error

Posted on 2011-09-16
5
227 Views
Last Modified: 2012-05-12
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
Comment
Question by:peter_coop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 250 total points
ID: 36548351
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
 

Author Comment

by:peter_coop
ID: 36548402
@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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36548477
Yes, I saw it later,
0
 

Author Comment

by:peter_coop
ID: 36548572
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
 

Author Closing Comment

by:peter_coop
ID: 36549353
Thank you
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question