We help IT Professionals succeed at work.
Get Started

MySql query not returning results

764 Views
Last Modified: 2014-03-09
I am trying to code a query whereby the query checks for duplicate items in a table and if found sends an error back to jquery. However, what is happening, is that jquery keeps giving me a typeError and I cannot see why. In my posted code, the code that is commented out, /* if ($box == 'DEMO111') works fine but not my query.

All mysql connections are working and I can connect to the db.

I would be grateful if someone could check my code and show me where I have gone wrong. Thanks

<?php 

$array = split('[,]', $_POST['box_add']);
$boxerrortext = 'No duplicate boxes';
?>

<?php

if (isset($_POST['submit'])) 	{
			$error = array();
			foreach ($array as $box) {
			$sql = "SELECT * FROM act WHERE item = '" . $box . "'";  
			$result = runSQL($sql) or die(mysql_error());
			$num_rows = mysql_num_rows($result);
			
			if ($num_rows) {
			//trigger_error('It exists.', E_USER_WARNING);
			
			$error[] = array('boxerror'=>$boxerrortext,
							'box'=>$box);
			$result = json_encode($error);

			echo $result;
			return;
			}
			}

			/* if ($box == 'DEMO111')
			
			{
			//echo 'There was an error somewhere';
			//$box = 'ERROR';

			//$error = array('boxerror'=>$boxerrortext, 'box'=>$box);
			//$output = json_encode($error);

			//echo $output;
			//return;
			} */
			
                       else {
	               
                       $form = array();

                       foreach ($array as $box) {
     
                       $form[] = array('dept'=>$dept, 
                      'company'=>$company,
                      'address'=>$address,
                      'service'=>$service,
                      'box'=>$box,
                      'destroydate'=>$destroydate,
                      'authorised'=>$authorised,
                      'submit'=>$submit);
     
$sql = "INSERT INTO `temp` (service, activity, department, company, address, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', '$destdate', NOW(), '$box', 1)";
$result = runSQL($sql) or die(mysql_error());
      }
	 }
   } 
          
        $result=json_encode($form);
     
        echo $result;

?>

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 48 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE