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?
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
Thanks - do you think there is something wrong with the values syntax........ e.g. ".$values["Manufacturer"]."
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):
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
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
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.
Thanks. It is selecting the correct value. i.e. if the Number Received = 4 the dump is showing 4 string(1) "4"
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
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.
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/
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?
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:
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: