1.11M Members

loop array insert into database

 
0
 

hi, my name is amin

this is output array $_POST

Array
(
    [status_batch] => Array
        (
            [1001] => Verify
            [1002] => Verify
            [2001] => Verify
            [2002] => Belum Batch
            [2003] => Belum Batch
            [2004] => Belum Batch
            [2005] => Belum Batch
            [2006] => Belum Batch
            [2007] => Belum Batch
            [2008] => Belum Batch
            [2009] => Belum Batch
            [1201] => Belum Batch
            [1202] => Belum Batch
            [2201] => Belum Batch
            [2202] => Belum Batch
            [2203] => Belum Batch
            [1101] => Belum Batch
            [2101] => Belum Batch
            [2102] => Belum Batch
            [2103] => Belum Batch
            [2104] => Belum Batch
            [2105] => Bersih
            [2106] => Belum Batch
            [2107] => Belum Batch
            [2108] => Belum Batch
        )

    [jadual] => kawalan
    [update] => update
)

code php & sql

$i=0;
foreach ($_POST[status_batch] as $papar) 
{
$myTable = $_POST[jadual]; 
//-------------------------------------------------------------------------------
$sql = "SELECT status_batch FROM ".$myTable." "; //echo "<hr><pre>$sql</pre><hr>";
$result = mysql_query($sql) or die(mysql_error()."<hr>$query<hr>"); 
$fields = mysql_num_fields ($result) or die("Num Fields Failed/ Nama Medan Takde");
$akhir=$fields-1; 
//-------------------------------------------------------------------------------
########################################################################
$ubah = " UPDATE $myTable SET \r";
for ( $f = 0 ; $f < $fields ; $f++ )
{  
$medan = mysql_field_name($result,$f); 
$ubah.=($f==$akhir) ? " $medan='$papar' \r":" $medan='$papar', \r"; 
}
$ubah.= " WHERE siri = '".$_POST[status_batch][$i++]."' 
 and ".$_POST[status_batch]." = '".$_POST[status_batch]."' 
";	 
########################################################################
echo "<pre>$ubah</pre><hr>";
//$result = mysql_query($ubah);
//header('Location:../sk/batch_sk.php?jadual=kawalan&item=10');
}

output

0)  UPDATE kawalan SET 
 status_batch='Verify' 
 WHERE siri = '' 
 and Array = 'Array' 

1)  UPDATE kawalan SET 
 status_batch='Verify' 
 WHERE siri = '' 
 and Array = 'Array' 

2)  UPDATE kawalan SET 
 status_batch='Verify' 
 WHERE siri = '' 
 and Array = 'Array' 

3)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

4)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

5)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

6)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

7)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

8)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

9)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

10)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

11)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

12)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

13)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

14)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

15)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

16)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

17)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

18)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

19)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

20)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

21)  UPDATE kawalan SET 
 status_batch='Bersih' 
 WHERE siri = '' 
 and Array = 'Array' 

22)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

23)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array' 

24)  UPDATE kawalan SET 
 status_batch='Belum Batch' 
 WHERE siri = '' 
 and Array = 'Array'

how i can get siri = 1001 and siri = 1002 and ....

 
0
 
WHERE siri = '".$_POST[status_batch][$i++]."' 
 and ".$_POST[status_batch]." = '".$_POST[status_batch]."'

Wouldn't work since $_POST is an array. Assign the value of $_POST to a variable and pass the index . For eg,

$arr=$_POST['status_batch'];
for ( $f = 0 ; $f < $fields ; $f++ ) {
echo $arr[$f];
//...
 
0
 

as nav33n pointed out, there are other issues going on in this script, but if you change your code like so, you should get the siri=xxx as you wanted...

foreach ($_POST[status_batch] as $key=> $papar) 
{
$myTable = $_POST[jadual]; 
//-------------------------------------------------------------------------------
$sql = "SELECT status_batch FROM ".$myTable." "; //echo "<hr><pre>$sql</pre><hr>";
$result = mysql_query($sql) or die(mysql_error()."<hr>$query<hr>"); 
$fields = mysql_num_fields ($result) or die("Num Fields Failed/ Nama Medan Takde");
$akhir=$fields-1; 
//-------------------------------------------------------------------------------
########################################################################
$ubah = " UPDATE $myTable SET \r";
for ( $f = 0 ; $f < $fields ; $f++ )
{  
$medan = mysql_field_name($result,$f); 
$ubah.=($f==$akhir) ? " $medan='$papar' \r":" $medan='$papar', \r"; 
}

$ubah.= " WHERE siri = '".$key."' 
 and ".$_POST[status_batch]." = '".$_POST[status_batch]."' 
";
 
0
 

as nav33n pointed out, there are other issues going on in this script, but if you change your code like so, you should get the siri=xxx as you wanted...

foreach ($_POST[status_batch] as $key=> $papar) 
{
$myTable = $_POST[jadual]; 
//-------------------------------------------------------------------------------
$sql = "SELECT status_batch FROM ".$myTable." "; //echo "<hr><pre>$sql</pre><hr>";
$result = mysql_query($sql) or die(mysql_error()."<hr>$query<hr>"); 
$fields = mysql_num_fields ($result) or die("Num Fields Failed/ Nama Medan Takde");
$akhir=$fields-1; 
//-------------------------------------------------------------------------------
########################################################################
$ubah = " UPDATE $myTable SET \r";
for ( $f = 0 ; $f < $fields ; $f++ )
{  
$medan = mysql_field_name($result,$f); 
$ubah.=($f==$akhir) ? " $medan='$papar' \r":" $medan='$papar', \r"; 
}

$ubah.= " WHERE siri = '".$key."' 
 and ".$_POST[status_batch]." = '".$_POST[status_batch]."' 
";

ooo, i want to say thank you johnsquibb and nav33n
$_POST[status_batch] as $key=> $papar
must use $key , but this $_POST is multiarray
:icon_lol:

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article