Hi I am trying to write an 'Before record added' event whereby the information submitted by the user is not added once to the database but is added the number of times selected in one of my drop down fields. So for example, the user fills in their info into the different fields and when they click submit, the record is added the number of times they have chosen in a drop down box (which is one of the fields), say like 1-50 times maybe. Here is my code...... it queries correctly but only adds the record once - whats wrong?: <?php // Before record added function BeforeAdd(&$values,&$message,$inline) { // Parameters: // $values - Array object. // Each field on the Add form is represented as a 'Field name'-'Field value' pair //********** Custom code ************ // put your custom code here for($i=1;$i<=$values["NUMBER_RECEIVED_IN_BATCH"];$i++) { mysql_query("Insert into STOCKCHECKER ('ID','SITE','DISCIPLINE','ITEM TYPE','BATCH/LOT NUMBER','EXPIRY DATE','NUMBER RECEIVED IN BATCH','RECEIVED BY','RECEIVED ON','PACKAGE INSERT CHECKED','USE COMMENCED BY','USE COMMENCED ON','TRANSFERRED BY','TRANSFERRED TO','SECOND SITE RECEIVED BY','NOTE/COMMENTS') VALUES ('".$i."','".$values["SITE"]."','".$values["DISCIPLINE"]."','".$values["ITEM_TYPE"]."','".$values["BATCH_LOT_NUMBER"]."','".$values["EXPIRY_DATE"]."','".$values["NUMBER_RECEIVED_IN_BATCH"]."','".$values["RECEIVED_BY"]."','".$values["RECEIVED_ON"]."','".$values["PACKAGE_INSERT_CHECKED"]."', '".$values["USE_COMMENCED_BY"]."','".$values["USE_COMMENCED_ON"]."','".$values["TRANSFERRED_BY"]."','".$values["TRANSFERRED_TO"]."','".$values["SECOND_SITE_RECEIVED_BY"]."','".$values["NOTE_COMMENTS"]."')"); } return true; // return true if you like to proceed with adding new record // return false otherwise } // function BeforeAdd ?> Code (markup):
Echo out $values["NUMBER_RECEIVED_IN_BATCH"] to make sure you're not trying to loop from zero to zero. (or 1 to zero for that matter) Also if your server is on PHP5, passing by reference (ie: &) might not work right. Not even sure why you need to pass by reference as you're not trying to change the value stored in the array. also I normally set the ID to be auto-increment, the problem with the above code is that since $i starts at 1, the second time you call that function its going to try to insert records where the ID numbers already exist.
HOw do you mean loop from zero to zero..... that field is my drop down and users can select only 1-50..... the ID field is autoincrement. My server is PHP 5.x ....so you reckon drop the '&'s ?
While that may true, we're assuming that the value was correctly stored in the array. In debugging its a good idea to have the function echo out the value to eliminate the assumption. Because if its not passing the value correctly, then there's the obviously problem as to why its only inserting once. From Zend: Essentially, unless you plan on changing the value of $values from the function, drop the & Also I would personally put some kind of error control in there, just in case there's a mysql error sometimes during the loop. Also since you don't seem to be incrementing the array at all... if someone chose 50, does 50 of the same record get inserted with just different ID's, since I noticed the associative position of the array always seems to be the same during the loop, its not for example $values[$i]["USE_COMMENCED_BY"]
Yes, if someone chose 50, the idea would be the same record is added 50 times with just a different ID value (autoincrement) ..... they would therefore differ only by the ID, and this is what I want as it is a stock program and the 50 entries would represent 50 of the same type of stock box received. I have been messing about with this for a week with no success!! can I be cheeky and you ask you kindly for a code suggestion!?
Your code won't auto-increment if you're defining the ID for it. Remove ('ID', and ($i, from the code, then MySQL will correctly autoincrement for you if you got that field set as such in MySQL.
right, I think im with you - leave this line in: for($i=1;$i<=$values["NUMBER_RECEIVED_IN_BATCH"];$i++) Code (markup): but edit the other bit so it reads: { mysql_query("Insert into STOCKCHECKER ('SITE','DISCIPLINE','ITEM TYPE','BATCH/LOT NUMBER','EXPIRY DATE','NUMBER RECEIVED IN BATCH','RECEIVED BY','RECEIVED ON','PACKAGE INSERT CHECKED','USE COMMENCED BY','USE COMMENCED ON','TRANSFERRED BY','TRANSFERRED TO','SECOND SITE RECEIVED BY','NOTE/COMMENTS') VALUES ('".$values["SITE"]."','".$values["DISCIPLINE"]."','".$values["ITEM_TYPE"]."','".$values["BATCH_LOT_NUMBER"]."','".$values["EXPIRY_DATE"]."','".$values["NUMBER_RECEIVED_IN_BATCH"]."','".$values["RECEIVED_BY"]."','".$values["RECEIVED_ON"]."','".$values["PACKAGE_INSERT_CHECKED"]."', '".$values["USE_COMMENCED_BY"]."','".$values["USE_COMMENCED_ON"]."','".$values["TRANSFERRED_BY"]."','".$values["TRANSFERRED_TO"]."','".$values["SECOND_SITE_RECEIVED_BY"]."','".$values["NOTE_COMMENTS"]."')"); } Code (markup): I would just try it but Im not at home.
Nope, still only adding the record once...... here is the latest code that doesnt work: // Before record added function BeforeAdd($values,$message,$inline) { // Parameters: // $values - Array object. // Each field on the Add form is represented as a 'Field name'-'Field value' pair //********** Custom code ************ // put your custom code here for($i=1;$i<=$values["NUMBER_RECEIVED_IN_BATCH"];$i++) { mysql_query("Insert into STOCKCHECKER ('SITE','DISCIPLINE','ITEM TYPE','BATCH/LOT NUMBER','EXPIRY DATE','NUMBER RECEIVED IN BATCH','RECEIVED BY','RECEIVED ON','PACKAGE INSERT CHECKED','USE COMMENCED BY','USE COMMENCED ON','TRANSFERRED BY','TRANSFERRED TO','SECOND SITE RECEIVED BY','NOTE/COMMENTS') VALUES ('".$values["SITE"]."','".$values["DISCIPLINE"]."','".$values["ITEM_TYPE"]."','".$values["BATCH_LOT_NUMBER"]."','".$values["EXPIRY_DATE"]."','".$values["NUMBER_RECEIVED_IN_BATCH"]."','".$values["RECEIVED_BY"]."','".$values["RECEIVED_ON"]."','".$values["PACKAGE_INSERT_CHECKED"]."','".$values["USE_COMMENCED_BY"]."','".$values["USE_COMMENCED_ON"]."','".$values["TRANSFERRED_BY"]."','".$values["TRANSFERRED_TO"]."','".$values["SECOND_SITE_RECEIVED_BY"]."','".$values["NOTE_COMMENTS"]."')"); } return true; // return true if you like to proceed with adding new record // return false otherwise } // function BeforeAdd Code (markup):
before the for loop, type in: echo $values["NUMBER_RECEIVED_IN_BATCH"]; and see if the value output to the screen is higher than 1