jwilliams Posted October 27, 2006 Share Posted October 27, 2006 Okay, here's what I'm trying to do...I'm trying to insert multiple arrays into the same mysql table at once. That works fine using foreach()...but only inserts the correct data for the array listed as the parameter in the foreach() loop.The other arrays get inserted, but with incorrect data, like the proper index for the array isn't being assigned or something...Here is my code:[code]<?php $myChildren = $_POST["myChildren"]; $myHours = $_POST["myHours"]; $myRate = $_POST["myRate"]; ##for each instance in our items array (checked items)...insert a new record foreach($_POST["myItems"] AS $key => $myItems){ $insertSQL = sprintf("INSERT INTO drs_invoices_items (invoiceID, itemID, childID, hours_logged, billable_rate) VALUES (%s, '$myItems', '$myChildren[$key]', '{$_POST["myHours"][$key]}', '{$_POST["myRate"][$key]}')", #used for invoiceID GetSQLValueString($_POST['invoiceID'], "int")); mysql_select_db($database_drs_database, $drs_database); $Result1 = mysql_query($insertSQL, $drs_database) or die(mysql_error()); }?>[/code]So the proper number of rows are inserted based on the $myItems array...But the other arrays ($myChildren, $myHours, $myRate), all get populated with data from other idexes in the array, and not from the samearray index as $myItems is pulled from.I've been trying everything, to no avail.I don't think I know enough about this stuff...Can somebody please help me?Thanks much in advance.Josiah Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2006 Share Posted October 27, 2006 I think it would be helpful if we could see the data you are working with. Try putting the following ont he page and post the response:[code]echo "<pre>";print_r($_POST["myItems"]);print_r($_POST["myChildren"]);print_r($_POST["myHours"]);print_r($myRate = $_POST["myRate"]);echo "</pre>";[/code] Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 27, 2006 Author Share Posted October 27, 2006 Okay, they are...in this order...myItems, myChildren, myHours, myRate[code]<?phpArray( [0] => 42 [1] => 41 [2] => 40 [3] => 39 [4] => 38 [5] => 37 [6] => 36 [7] => 35 [8] => 34 [9] => 33 [10] => 32 [11] => 31 [12] => 30 [13] => 29 [14] => 28 [15] => 27 [16] => 26 [17] => 25 [18] => 21 [19] => 22)Array( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 25 [13] => 26 [14] => 26 [15] => 26 [16] => 0 [17] => 0 [18] => 0 [19] => 0)Array( [0] => 0.02 [1] => 0.62 [2] => 1.99 [3] => 0.66 [4] => 1.02 [5] => 0.34 [6] => 0.04 [7] => 0.08 [8] => 0.02 [9] => 0.08 [10] => 0.03 [11] => 9.02 [12] => 0.68 [13] => 0.79 [14] => 1.14 [15] => 1.25 [16] => 1.15 [17] => 1.00 [18] => 2.00 [19] => 1.00)Array( [0] => 100 [1] => 100 [2] => 100 [3] => 100 [4] => 100 [5] => 100 [6] => 100 [7] => 100 [8] => 100 [9] => 100 [10] => 100 [11] => 100 [12] => 100 [13] => 100 [14] => 100 [15] => 100 [16] => 100 [17] => 100 [18] => 100 [19] => 100)?>[/code]If I insert all of these arrays everything works fine...but if I select only a few in the middle (based on check boxes from a form) and then try to insert just those items, all of the correct $myItems come through, but the others iterate from 0, and the data doesn't match up.What can you make of this?Thanks for your help!Josiah Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2006 Share Posted October 27, 2006 So, the arrays are passed from the form? Is the myItems array the list of checkbox items that were checked? This should be pretty easy to sort out, just need to understand how the data is passed to this page. Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 27, 2006 Author Share Posted October 27, 2006 Yes, the arrays are passed from the form.myItems[] is the array of all checkboxes that are checkedmyRate, myChildren and myHours are all hidden fields residing in the same row as the checkbox, and are made into arrays as well.Thank you for checking back frequently! Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2006 Share Posted October 27, 2006 OK, that won't work. When you use checkboxes, only the checkboxes that are checked get passed. So, if you have 20 checkboxes and give them the same name (myItems[]) so that they are passed as an array, if you only check 5 items then the passed array only has 5 items in it, with indexes of 0-4.In your example you should be getting values, but they should be the wrong values.Can you put the myItems array in a hidden field as well? Then create your checkboxes with another array name (let's say checkedItems) and give them the values of the indexes (0-19). Then you could get your values like this:[code]<?php$myChildren = $_POST["myChildren"];$myHours = $_POST["myHours"];$myRate = $_POST["myRate"];$myItems = $_POST["myItems"];$checkedItems = $_POST["checkedItems"];##for each instance in our items array (checked items)...insert a new recordforeach($checkedItems as $key){ $insertSQL = sprintf("INSERT INTO drs_invoices_items (invoiceID, itemID, childID, hours_logged, billable_rate) VALUES (%s, '$myItems[$key]', '$myChildren[$key]', '{$_POST["myHours"][$key]}', '{$_POST["myRate"][$key]}')", #used for invoiceID GetSQLValueString($_POST['invoiceID'], "int")); mysql_select_db($database_drs_database, $drs_database); $Result1 = mysql_query($insertSQL, $drs_database) or die(mysql_error());}?>[/code] Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 27, 2006 Author Share Posted October 27, 2006 Thanks so much for your help on this....I think we are ALMOST there.I implemented what you suggested, and here are my results:I checked the following items on my form:[code]<?phpArray - myItems( [14] => 28 [16] => 26 [18] => 21 [19] => 22)Array - myChildren( [14] => 26 [16] => 0 [18] => 0 [19] => 0)Array - myHours( [14] => 1.14 [16] => 1.15 [18] => 2.00 [19] => 1.00)Array - myRate( [14] => 100 [16] => 100 [18] => 100 [19] => 100)?>However, the following information went into my database:<?phpArray - myItems - ALL DATA CORRECT( [14] => 28 [16] => 26 [18] => 21 [19] => 22)Array - myChildren - DATA INCORRECT( [14] => 0 --- should be 26 [16] => 0 [18] => 0 [19] => 0)Array - myHours( [14] => .02 -- should be 1.14 [16] => .62 -- should be 1.15 [18] => 1.99 -- should be 2.00 [19] => .66 -- should be 1.00)Array - myRate( [14] => 100 [16] => 100 [18] => 100 [19] => 100)?>[/code]So what i don't understand is why the myItems value is being carried over correctly and the rest are not?? Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 27, 2006 Author Share Posted October 27, 2006 So as you can see...after it properly selects myItems, it then adds these next indexes into the database:Example of array data entered into database (from myHours array)[code]<?phpArray( [0] => 0.02 [1] => 0.62 [2] => 1.99 [3] => 0.66)?>[/code]So it's not properly selecting the index from checkedItems, rather iterating through $key, which starts at zero.Any idea how to fix this?Thanks again Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 27, 2006 Author Share Posted October 27, 2006 I just realized that my checkedItems array is iterating backwards...so where myRow[0]::checkedItems[19]could this be my problem? Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 27, 2006 Author Share Posted October 27, 2006 or it could be that now all 20 items from each array is being inserted into the database instead of just the items i've checked. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2006 Share Posted October 27, 2006 I think you need to put in some debugging code so that when you submit the form it prints out1) The checked values 2) The complete arrays3) The sql query Quote Link to comment Share on other sites More sharing options...
sasa Posted October 27, 2006 Share Posted October 27, 2006 you have problems with your formcan we see form code Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 30, 2006 Author Share Posted October 30, 2006 Okay, the form gets a bit confusing because i'm generating it on the fly with Javascript DOM, but I will post to see if you guys can make heads or tails of it.So here is the main form, that lists all projects associated with a client, currently there is a checkbox next to each project (this is not the checkbox i'm having trouble with).This form lists projects...if you press a button within this form, it drills down and lists all ITEMS associated with that project. This is all done dynamically with javascript. PHP passes values to my javascript function, which then generates a row for each item and lists the items under each project. Next to each item is a check box, these are the checkboxes i'm having problems with. If you check several boxes and hit submit, it grabs the correct values for the checkbox and the $myItems, but for nothing else. I will start by posting the main projects form here:[code]<form action="<?php echo $editFormAction; ?>" method="POST" name="projects"> <table id="myTable" width="775" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class="columnHeaders"> <td width="25"> </td> <td width="250">project</td> <td width="300">description</td> <td width="75">hours</td> <td width="75">rate</td> <td width="150">total</td> </tr> <?php #alternate row colors $current_row = 0; $uniqueID = 0; do { $current_row = 1 - $current_row; $uniqueID++; ?> <tr id="row<?php echo $row_selectProjects['projectID']; ?>" class="row<?php echo $current_row; ?>"> <td><input name="projects" type="checkbox" value="1"> <input name="myRowID" type="hidden" id="row<?php echo $row_selectProjects['projectID']; ?>" value="row<?php echo $row_selectProjects['projectID']; ?>"><input name="projectID" type="hidden" value="<?php echo $row_selectProjects['projectID']; ?>"></td> <td height="25"><a href="javascript:showItems('<?php echo $myID; ?>','<?php echo $row_selectProjects['projectID'] ; ?>', '<?php echo $uniqueID; ?>');"><img src="images/carrot.gif" width="10" height="15" border="0"></a><?php echo $row_selectProjects['project_name']; ?></td> <td><?php echo $row_selectProjects['project_desc']; ?></td> <td><?php echo $row_selectProjects['total_hours_converted']; ?></td> <td><?php echo $row_selectProjects['billable_rate']; ?></td> <td>$<?php echo $row_selectProjects['total_billable']; ?></td> </tr> <?php $cellID = $row_selectItems['itemID'] ; ?> <?php } while ($row_selectProjects = mysql_fetch_assoc($selectProjects)); ?> <tr> <td colspan="6"><img src="images/horz_line.gif" width="100%" height="7"> </td> </tr> <tr> <td colspan="6"><input name="clientID" type="hidden" value="<?php echo $myID; ?>"> <input name="invoiceID" type="hidden" value="<?php echo $myInsertID; ?>"> <input name="myDate" type="hidden" value="<?php echo $myDate; ?>"><input name="" type="submit" value="Add to Invoice"> </td> </tr> </table> <input type="hidden" name="MM_insert" value="projects"></form>[/code] Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 30, 2006 Author Share Posted October 30, 2006 So as you can see, there is a link to a javascript function within that form, here is the code pulled out for your reference:[code]<a href="javascript:showItems('<?php echo $myID; ?>','<?php echo $row_selectProjects['projectID'] ; ?>', '<?php echo $uniqueID; ?>');"><img src="images/carrot.gif" width="10" height="15" border="0"></a>[/code]and here is the showItems() function[code]function showItems(myID, itemID, rowID){ location.href = 'add_invoices10.php?recordID='+myID+'&itemID='+itemID+'&rowID='+rowID+'&step=3'; }[/code] Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 30, 2006 Author Share Posted October 30, 2006 It's not letting me paste rest of code...i will post a preview online. Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 30, 2006 Author Share Posted October 30, 2006 Okay, go to this page to see the form and the code:http://www.drs-flash.com/drs/intranet/add_invoices10.phpuser: testpass: testIn STEP 1, select DV TIME STUDIOSIn STEP 2, press the Generate Invoice ButtonIn STEP 3, click the blue carrot next to DV Time Studios to drill downSelect some items, and then press Add to InvoiceThanksJosiah Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 30, 2006 Share Posted October 30, 2006 There is no "Generate Invoce" button, only an "Add to Invoice" and it generates errors. Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 30, 2006 Author Share Posted October 30, 2006 Make sure you select a CLIENT from the drop down in step 1 first...Select DVTIME STUDIOS...then the GENERATE INVOICES button will appear Quote Link to comment Share on other sites More sharing options...
jwilliams Posted October 30, 2006 Author Share Posted October 30, 2006 Okay guys, thanks for all of your help...I've figured it out...it was a problem with my form.It was a careless overlook...my checkbox field was receiving the wrong value..instead of receiving the $checkedItems value it was receiving the $myItems value.When i fixed this, the correct number of rows are inserted every time with the correct data!Thank you for all of your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.