bradynorman Posted August 12, 2008 Share Posted August 12, 2008 Hi, I am trying to adapt the following code so that as each line is processed, the variable $bin is correctly updated. Obviously the code is failing, any ideas please? mysql_select_db($database_grcdb, $grcdb); foreach($id as $val) { $query = "UPDATE stock SET status = 'warehouse' SET bin = '$bin' WHERE id = '$val'"; mysql_query($query) or die(mysql_error()); } mysql_close(); $updateGoTo = "stock.php"; header(sprintf("Location: %s", $updateGoTo)); cheers Mike Quote Link to comment Share on other sites More sharing options...
Zhadus Posted August 12, 2008 Share Posted August 12, 2008 Where is $bin assigned and how is it failing? Is it not updating or are you just resulting in nothing under bin in the db? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 12, 2008 Share Posted August 12, 2008 Where is the variables $id and $bin set? What are you trying to do. Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 You have 'SET' twice in your query. You should be doing it like this anyways mysql_select_db($database_grcdb, $grcdb); $query = "UPDATE `stock` SET `status` = 'warehouse', `bin` = '$bin' WHERE `id` = '" . implode( "' AND `id` = '", $id ) . "'"; mysql_query($query) or die(mysql_error()); mysql_close($grcdb); $updateGoTo = "stock.php"; header(sprintf("Location: %s", $updateGoTo)); All in one query Quote Link to comment Share on other sites More sharing options...
bradynorman Posted August 12, 2008 Author Share Posted August 12, 2008 Thanks for that, The variables are id[] and bin[], but i am still struggling on how to insert the $bin variable on each loop cheers Mike Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 12, 2008 Share Posted August 12, 2008 What is $bin? Is it a form field, a url variable, Session variable or what? if its either of the above then $bin should be in superglobal form, eg $_POST['bin'] or $_GET['bin'] or $_SESSION['bin'] etc. $bin will only work if register_globals is enabled. This setting is disabled by default as it is depreciated and is being removed as of PHP6. Quote Link to comment Share on other sites More sharing options...
bradynorman Posted August 12, 2008 Author Share Posted August 12, 2008 $bin and $id are form variables. In the form they are submitted as bin[] and id[] as there are multiple rows to process. Previously the statement was; mysql_select_db($database_grcdb, $grcdb); foreach($id as $val) { $query = "UPDATE stock SET status = 'warehouse' WHERE id = '$val'"; mysql_query($query) or die(mysql_error()); } mysql_close(); $updateGoTo = "stock.php"; header(sprintf("Location: %s", $updateGoTo)); but i now what to also update the field 'bin' with the posted variable which is different for each row. cheers Mike Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 Oh, bin is an array! <?php if ( count($bin) != count($id) ) die( 'Array counts for $bin and $id do not match' ); /* This will assume $id[x] => $bin[x], and $id and $bin are properly formatted arrays. If they're not, then use the below code foreach ( $bin as $val ) $holdBin[] = $val; foreach ( $id as $val ) $holdId[] = $val; $bin = $holdBin; $id = $holdId; unset( $holdBin, $holdId ); */ for ( $i = 0, $c = count($id); $i < $c; $i++ ) { $q = "UPDATE `stock` SET `status` = 'warehouse', `bin` = '{$bin[$i]}' WHERE `id` = '{$id[$i]}'"; mysql_query( $q ); } ?> Quote Link to comment Share on other sites More sharing options...
bradynorman Posted August 12, 2008 Author Share Posted August 12, 2008 ok i have updated the code to the following: <?php if ( count($bin) != count($id) ) die( 'Array counts for $bin and $id do not match' ); foreach ( $bin as $val ) $holdBin[] = $val; foreach ( $id as $val ) $holdId[] = $val; $bin = $holdBin; $id = $holdId; unset( $holdBin, $holdId ); for ( $i = 0, $c = count($id); $i < $c; $i++ ) { $q = "UPDATE `stock` SET `status` = 'warehouse', `bin` = '{$bin[$i]}' WHERE `id` = '{$id[$i]}'"; mysql_query( $q ); } mysql_close(); $updateGoTo = "stock.php"; header(sprintf("Location: %s", $updateGoTo)); ?> the code runs, but nothing updates! Previously, using foreach the sql was performed and everything was updated. Am i missing something, surely that was a loop can i not modify the initial statement? cheers Mike Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 12, 2008 Share Posted August 12, 2008 mysql_query( $q ) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
efficacious Posted August 12, 2008 Share Posted August 12, 2008 I like to do that like this if(mysql_query($q)) { echo('Success');//Success } else { echo('FAIL'.mysql_error()); } 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.