Jump to content

MySql code help needed


orpheus

Recommended Posts

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
Share on other sites

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 [])

Edited by MadTechie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.