Jump to content

[SOLVED] Foreach statement


bradynorman

Recommended Posts

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

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

 

Link to comment
Share on other sites

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 );
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.