1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Whats wrong with my code?

Discussion in 'Programming' started by NITRO23456, Aug 26, 2017.

  1. #1
    Hi guys

    Trying to get this to work. What I want it to do is take the number value from 'Number Received' and add the record that many times:

    
                    // Before record added
    function BeforeAdd(&$values, &$message, $inline)
    {
    
          
    $count = $values["Number Received"];
    
        for($i = 1; $i < $count; $i++)
        {
            $query = "INSERT INTO Stock
            (`Hospital`,`Manufacturer`,`Department`,`Item Type`,`Batch/Lot Number`,`Expiry Date`,`Number Received`,`Received By`,`Received On`,`Package Insert/Condition Checked`,`Use Commenced By`,`Use Commenced On`,`Notes/Comments`, `Value`)
            VALUES
            (".$values["Hospital"].",".$values["Manufacturer"].",".$values["Department"].",".$values["Item Type"].",".$values["Batch/Lot Number"].",".$values["Expiry Date"].",".$values["Number Received"].",".$values["Received By"].",".$values["Received On"].",".$values["Package Insert/Condition Checked"].",".$values["Use Commenced By"].",".$values["Use Commenced On"].",".$values["Notes/Comments"].",".$values["Value"].");";
                      
           mysql_query($query);
        }
        return true;
    
    
    ;      
    } // function BeforeAdd
    
    
    PHP:
    But I just get a type 2 MySQL error.

    Whats wrong?
    SEMrush
     
    Last edited by a moderator: Aug 26, 2017
    NITRO23456, Aug 26, 2017 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #2
    first thing to do is to echo out the query and run it in phpmhyadmin or sqlyog or whatever you use and see what the error message is.

    The other thing to consider is building up the string so you do just one insert statement for many lines.

    
    INSERT INTO clients (client_name, telephone)
    VALUES('Marie & Associates', '504-486-1234'),
    ('Geoffrey & Company', '617-522-1234'),
    ('Kenneth & Partners', '617-523-1234');
    PHP:
    ref: http://mysqlresources.com/documentation/data-manipulation/insert-multiple-row-insertions
     
    sarahk, Aug 26, 2017 IP
  3. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks - do you think there is something wrong with the values syntax........ e.g.
    ".$values["Manufacturer"]."
     
    NITRO23456, Aug 26, 2017 IP
  4. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    OK, this works........

    
        // Before record added
    function BeforeAdd(&$values, &$message, $inline)
    {
    
         
    $count = $values["Number Received"];
    
        for($i = 1; $i < $count; $i++)
        {
            $query = "INSERT INTO Stock
            (`Hospital`,`Manufacturer`,`Department`,`Item Type`,`Batch/Lot Number`,`Expiry Date`,`Number Received`,`Received By`,`Received On`,`Package Insert/Condition Checked`,`Use Commenced By`,`Use Commenced On`,`Notes/Comments`, `Value`)
            VALUES
            (".$values["Hospital"].",".$values["Manufacturer"].",".$values["Department"].",".$values["Item Type"].",".$values["Batch/Lot Number"].",".$values["Expiry Date"].",".$values["Number Received"].",".$values["Received By"].",".$values["Received On"].",".$values["Package Insert/Condition Checked"].",".$values["Use Commenced By"].",".$values["Use Commenced On"].",".$values["Notes/Comments"].",".$values["Value"].");";
                     
           mysql_query($query);
        }
        return true;
    
    
    ;     
    } // function BeforeAdd
    
    Code (markup):
    but only for quantities of 1............. in other words 'Number Received' = 1

    Number Received is selected from a dropdown box 1-50...... there must be something wrong with the way it original script is grabbing the number from the field?

    If someone could help me with this bit it would be most appreciated:

    $count = $values["Number Received"];
    
    for($i = 1; $i < $count; $i++)
    Code (markup):
     
    Last edited: Aug 26, 2017
    NITRO23456, Aug 26, 2017 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #5
    echo out what you're getting in that value. I'm guessing there's something unexpected in there.

    I've just looked at your column names - great that you've got backticks around them but you don't, surely, have spaces and special characters in column names do you?
    `Batch/Lot Number`,`Expiry Date`
    Code (markup):
    I've also never tried it but I'm uncomfortable about the spaces in the array key names too
     
    sarahk, Aug 26, 2017 IP
  6. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Yeah there are spaces etc, never been a problem before when using back ticks - as it works when the Number Received = 1, I dont think that is an issue.

    Must be something to do with how it is getting values greater than 1 from Number Received

    Not sure how to echo out as I am using a php generator and script is in an include script
     
    NITRO23456, Aug 26, 2017 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #7
    at the top of the code just put in
    var_dump($values["Number Received"]);
    PHP:
    or if the generator is wiping out debug type content I do this
    mail('me@gmail.com','Number Received: '.$values["Number Received"].' - '.time(),'dummy body text');
    PHP:
    I put the time() in so that if I run it more than once they don't get stacked like a conversation.
     
    sarahk, Aug 26, 2017 IP
  8. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Thanks. It is selecting the correct value. i.e. if the Number Received = 4 the dump is showing 4

    string(1) "4"
     
    NITRO23456, Aug 26, 2017 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #9
    try putting an intval() around it
     
    sarahk, Aug 26, 2017 IP
  10. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #10
    which bit?
     
    NITRO23456, Aug 26, 2017 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #11
    The number received, it's a string, which I'd have thought would be ok because PHP is pretty loose on it's type matching. However it's worth a go
     
    sarahk, Aug 26, 2017 IP
  12. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #12
    No improvement I'm afraid. Weird that it works when the dropdown for Number Received is 1, but when 2 or more is selected from the dropdown field it errors.
     
    NITRO23456, Aug 26, 2017 IP
  13. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #13
    So what errors?
     
    sarahk, Aug 26, 2017 IP
  14. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #14
    This is the error I am getting

    Line 49 is the } at the end:

    }
    return true;
    [​IMG]
     
    NITRO23456, Aug 26, 2017 IP
  15. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #15
    I'd be looking at upgrading your connection to mysql - get a reliable library. Your mysql_query should have an "or die" catcher on the end. The error is a basic connectivity error which makes me wonder if the first query is killing the connection somehow.

    Google threw up this in first place, might be worth a go: https://meekro.com/
     
    sarahk, Aug 26, 2017 IP
  16. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #16
    Hi - I have started a new database on a newer version of MySQL (5.5 - this is the most up to date offered by my host) and this has solved the SQL query in my other thread.

    It has also stopped the error happening above, but the record only adds once still. In other words, if Number Received = 1 or if it = 25 it still only adds the record once. If 25 is selected it is supposed to add the record 25 times.... but it adds it once with no errors.

    Any ideas? Must be the code is wrong?
     
    NITRO23456, Aug 27, 2017 IP
  17. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #17
    Can you suggest an alternative way?
     
    NITRO23456, Aug 27, 2017 IP
  18. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #18
    have you put in an "or die(mysql_error())" type of statement on the insert to see if there's something killing it there.

    Assuming you don't want to use a database library and you want to continue using mysql_query try this

    
     <?php
     // Before record added
     function BeforeAdd($values, $message, $inline){
    
        $count = $values["Number Received"];
        var_dump($count);
        $values = array();
    
        for($i = 0; $i < $count; $i++)    {
    
            $values[] = "('{$values['Hospital']}'
            ,'{$values["Manufacturer"]}'
            ,'{$values["Department"]}'
            ,'{$values["Item Type"]}'
            ,'{$values["Batch/Lot Number"]}'
            ,'{$values["Expiry Date"]}'
            ,'{$values["Number Received"]}'
            ,'{$values["Received By"]}''
            ,'{$values["Received On"]}'
            ,'{$values["Package Insert/Condition Checked"]}'
            ,'{$values["Use Commenced By"]}'
            ,'{$values["Use Commenced On"]}'
            ,'{$values["Notes/Comments"]}'
            ,'{$values["Value"]}');";
        }
        var_dump($values);
        $query = "INSERT INTO `Stock`
        (`Hospital`,`Manufacturer`,`Department`,`Item Type`,`Batch/Lot Number`,`Expiry Date`,`Number Received`,`Received By`,`Received On`,`Package Insert/Condition Checked`,`Use Commenced By`,`Use Commenced On`,`Notes/Comments`, `Value`)
        VALUES ".implode(' , ', $values);
    
        mysql_query($query) or die(mysql_error(). '<br />'.$query);
        return true;
    
    } // function BeforeAdd
    PHP:
     
    sarahk, Aug 27, 2017 IP
  19. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    503
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #19
    Getting this

     
    NITRO23456, Aug 27, 2017 IP
  20. sarahk

    sarahk iTamer Staff

    Messages:
    23,636
    Likes Received:
    3,190
    Best Answers:
    89
    Trophy Points:
    615
    #20
    d'oh

    I reused the $values variable name. Try this

    <?php
    // Before record added
    function BeforeAdd($values, $message, $inline){
    
    $count = $values["Number Received"];
    var_dump($count);
    $queryValues = array();
    
    for($i = 0; $i < $count; $i++) {
    
    $queryValues[] = "('{$values['Hospital']}'
    ,'{$values['Manufacturer']}'
    ,'{$values['Department']}'
    ,'{$values['Item Type']}'
    ,'{$values['Batch/Lot Number']}'
    ,'{$values['Expiry Date']}'
    ,'{$values['Number Received']}'
    ,'{$values['Received By']}''
    ,'{$values['Received On']}'
    ,'{$values['Package Insert/Condition Checked']}'
    ,'{$values['Use Commenced By']}'
    ,'{$values['Use Commenced On']}'
    ,'{$values['Notes/Comments']}'
    ,'{$values['Value']}')";
    }
    var_dump($values);
    $query = "INSERT INTO `Stock`
    (`Hospital`,`Manufacturer`,`Department`,`Item Type`,`Batch/Lot Number`,`Expiry Date`,`Number Received`,`Received By`,`Received On`,`Package Insert/Condition Checked`,`Use Commenced By`,`Use Commenced On`,`Notes/Comments`, `Value`)
    VALUES ".implode(' , ', $queryValues);
    
    mysql_query($query) or die(mysql_error(). '<br />'.$query);
    return true;
    
    } // function BeforeAdd
    PHP:
     
    sarahk, Aug 27, 2017 IP