orpheus Posted March 4, 2014 Share Posted March 4, 2014 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 DB Config <?php function runSQL($rsql) { $hostname = "localhost"; $username = "root"; $password = ""; $dbname = "logistor_logistor"; $connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database"); $db = mysql_select_db($dbname); $result = mysql_query($rsql) or die (mysql_error()); return $result; mysql_close($connect); ?> <?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; ?> Link to comment https://forums.phpfreaks.com/topic/286698-mysql-code-help-needed/ Share on other sites More sharing options...
MadTechie Posted March 4, 2014 Share Posted March 4, 2014 First thing I would remove the whitespace, between the php tags $boxerrortext = 'No duplicate boxes'; ?> <?php if (isset($_POST['submit'])) { but i think the main problem is with the array itself $error[] = array( 'boxerror'=>$boxerrortext, 'box'=>$box ); should probably be $error = array( 'boxerror'=>$boxerrortext, 'box'=>$box ); (no []) Link to comment https://forums.phpfreaks.com/topic/286698-mysql-code-help-needed/#findComment-1471450 Share on other sites More sharing options...
Skewled Posted March 5, 2014 Share Posted March 5, 2014 $form[] = array('dept'=>$dept, 'company'=>$company, 'address'=>$address, 'service'=>$service, 'box'=>$box, 'destroydate'=>$destroydate, 'authorised'=>$authorised, 'submit'=>$submit); should also not have the [ ] Link to comment https://forums.phpfreaks.com/topic/286698-mysql-code-help-needed/#findComment-1471534 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.