iwpg Posted June 10, 2008 Share Posted June 10, 2008 This has got me stumped. I am trying to update a table where a record ID = to the post ID with values. No luck at all. This is what I have so far: $commissions = preg_split('/\r\n|\n/', $_POST['commission'], -1, PREG_SPLIT_NO_EMPTY); $id = preg_split('/\r\n|\n/', $_POST['id'], -1, PREG_SPLIT_NO_EMPTY); //Iterate through array foreach ($data as $val) { mysql_query("insert into $table (commissions) values ('$commissions') WHERE id ='$id'"); } My form looks like: <input type="text" name="id" id="id[]" size="7" value="3"> <input type="text" name="commission" id="commission[]" size="15" value="100.00"> Any help will be GREATLY appreciated. Link to comment https://forums.phpfreaks.com/topic/109519-solved-php-mysql-array-update-using-where/ Share on other sites More sharing options...
btherl Posted June 10, 2008 Share Posted June 10, 2008 This and this page explain how to use arrays with forms in php. Alternatively you can parse the post data yourself, which I did before I realized that php can do it for me But if you do like you are doing, you will only get the LAST item in $_POST, not all of them. To get all of them you must name your input as "id[]" and "commission[]" Link to comment https://forums.phpfreaks.com/topic/109519-solved-php-mysql-array-update-using-where/#findComment-561794 Share on other sites More sharing options...
iwpg Posted June 10, 2008 Author Share Posted June 10, 2008 Thanks for your help. I looked everywhere for update query examples on arrays in MYSQL, but didn't have any luck. Do I need to loop the $id variable through the array like the $commissions variable? I'm just kindof stuck with this part. ??? Link to comment https://forums.phpfreaks.com/topic/109519-solved-php-mysql-array-update-using-where/#findComment-562165 Share on other sites More sharing options...
btherl Posted June 11, 2008 Share Posted June 11, 2008 Hmm, well in that situation you have two arrays, and you need to loop through both at one time. That's a little more complex than a single array. $commission_arr = $_POST['commission']; $id_arr = $_POST['id']; if (!is_array($commission_arr) || !is_array($id_arr)) { die("Either commission or id wasn't an array!"); } foreach ($id_arr as $key => $id) { $commission = $commission_arr[$key]; $sql = "insert into $table (commissions) values ('$commission') WHERE id ='$id'"; mysql_query($sql) or die("Error in $sql: " . mysql_error()); } Here I'm assuming that the indexes for the id and commission arrays will match up, which they should in your case. Link to comment https://forums.phpfreaks.com/topic/109519-solved-php-mysql-array-update-using-where/#findComment-562684 Share on other sites More sharing options...
iwpg Posted June 14, 2008 Author Share Posted June 14, 2008 That really helped me out That did the trick! I'm getting better at arrays, but it's taking a while. Thanks a whole lot again! Link to comment https://forums.phpfreaks.com/topic/109519-solved-php-mysql-array-update-using-where/#findComment-565446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.