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
https://forums.phpfreaks.com/topic/119296-solved-foreach-statement/
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

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.

$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

 

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

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.