jaxdevil Posted October 10, 2008 Share Posted October 10, 2008 Below is the code I am trying. Have not used loops very much so someone else will be able to easily figure this out. I have a form that posts the data to this page. The from fields are in the first code box below and the second is the update query where I am trying to use the loops. It is not right, breaks the code each time so I am not formatting something right: ?> <input type="hidden" name="start" value="<?=$start?>"> <input type="hidden" name="end" value="<?=$end?>"> <input type="hidden" name="id[]" value="<?=$id?>"> <input type="hidden" name="scan_code[]" value="<?=$scan_code?>"> <? require($_SERVER['DOCUMENT_ROOT'].'/includes/configs/sql_connect.php'); for ($x = $start; $x >= $end; $x++) { $query="UPDATE inventory SET `scan_code` = '$scan_code[]' WHERE id='$id[]'"; $result =mysql_query($query) or die ("Problem with the query $query<br>: " . mysql_error()); } // end for ?> Link to comment https://forums.phpfreaks.com/topic/127861-solved-problem-with-loop-and-sql-update/ Share on other sites More sharing options...
F1Fan Posted October 10, 2008 Share Posted October 10, 2008 I see two problems, and they both have to do with the arrays in your query. 1st, you're not using a key in the array, instead just [] ($scan_code[]) and you're not using {} brackets around the arrays. Link to comment https://forums.phpfreaks.com/topic/127861-solved-problem-with-loop-and-sql-update/#findComment-661969 Share on other sites More sharing options...
Maq Posted October 10, 2008 Share Posted October 10, 2008 Have you defined $start and $end yet? If not you need to use the POST methods. for ($x = $_POST_['start']; $x >= $_POST['end']; $x++) { Link to comment https://forums.phpfreaks.com/topic/127861-solved-problem-with-loop-and-sql-update/#findComment-661970 Share on other sites More sharing options...
jaxdevil Posted October 10, 2008 Author Share Posted October 10, 2008 Thanks. I think I found something that would make sense, a foreach loop, but how can I define it with 2 post infos? I.e I have $id AND $scan_code and this would only work for looping base don ID: foreach($id as $ids) { echo "$ids <br />"; } So how could I use that for both $id AND $scan_code ? Link to comment https://forums.phpfreaks.com/topic/127861-solved-problem-with-loop-and-sql-update/#findComment-661971 Share on other sites More sharing options...
F1Fan Posted October 10, 2008 Share Posted October 10, 2008 <?php foreach($id as $k=>$ids) { echo "$ids <br />"; echo "{$scan_code[$k]} <br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/127861-solved-problem-with-loop-and-sql-update/#findComment-661995 Share on other sites More sharing options...
jaxdevil Posted October 10, 2008 Author Share Posted October 10, 2008 You da man! Thanks! Link to comment https://forums.phpfreaks.com/topic/127861-solved-problem-with-loop-and-sql-update/#findComment-662019 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.