User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,532 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,340 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1731 | Replies: 3
Reply
Join Date: Oct 2007
Posts: 6
Reputation: amin007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amin007 amin007 is offline Offline
Newbie Poster

Question loop array insert into database

  #1  
Feb 13th, 2008
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 ....
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: loop array insert into database

  #2  
Feb 13th, 2008
  1. WHERE siri = '".$_POST[status_batch][$i++]."'
  2. and ".$_POST[status_batch]." = '".$_POST[status_batch]."'
Wouldn't work since $_POST['status_batch'] is an array. Assign the value of $_POST['status_batch'] to a variable and pass the index . For eg,
  1. $arr=$_POST['status_batch'];
  2. for ( $f = 0 ; $f < $fields ; $f++ ) {
  3. echo $arr[$f];
  4. //...
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Nov 2007
Location: Las Vegas, Nevada
Posts: 83
Reputation: johnsquibb is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: loop array insert into database

  #3  
Feb 13th, 2008
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...

  1. foreach ($_POST[status_batch] as $key=> $papar)
  2. {
  3. $myTable = $_POST[jadual];
  4. //-------------------------------------------------------------------------------
  5. $sql = "SELECT status_batch FROM ".$myTable." "; //echo "<hr><pre>$sql</pre><hr>";
  6. $result = mysql_query($sql) or die(mysql_error()."<hr>$query<hr>");
  7. $fields = mysql_num_fields ($result) or die("Num Fields Failed/ Nama Medan Takde");
  8. $akhir=$fields-1;
  9. //-------------------------------------------------------------------------------
  10. ########################################################################
  11. $ubah = " UPDATE $myTable SET \r";
  12. for ( $f = 0 ; $f < $fields ; $f++ )
  13. {
  14. $medan = mysql_field_name($result,$f);
  15. $ubah.=($f==$akhir) ? " $medan='$papar' \r":" $medan='$papar', \r";
  16. }
  17.  
  18. $ubah.= " WHERE siri = '".$key."'
  19. and ".$_POST[status_batch]." = '".$_POST[status_batch]."'
  20. ";
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: amin007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amin007 amin007 is offline Offline
Newbie Poster

Re: loop array insert into database

  #4  
Feb 13th, 2008
Originally Posted by johnsquibb View Post
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...

  1. foreach ($_POST[status_batch] as $key=> $papar)
  2. {
  3. $myTable = $_POST[jadual];
  4. //-------------------------------------------------------------------------------
  5. $sql = "SELECT status_batch FROM ".$myTable." "; //echo "<hr><pre>$sql</pre><hr>";
  6. $result = mysql_query($sql) or die(mysql_error()."<hr>$query<hr>");
  7. $fields = mysql_num_fields ($result) or die("Num Fields Failed/ Nama Medan Takde");
  8. $akhir=$fields-1;
  9. //-------------------------------------------------------------------------------
  10. ########################################################################
  11. $ubah = " UPDATE $myTable SET \r";
  12. for ( $f = 0 ; $f < $fields ; $f++ )
  13. {
  14. $medan = mysql_field_name($result,$f);
  15. $ubah.=($f==$akhir) ? " $medan='$papar' \r":" $medan='$papar', \r";
  16. }
  17.  
  18. $ubah.= " WHERE siri = '".$key."'
  19. and ".$_POST[status_batch]." = '".$_POST[status_batch]."'
  20. ";


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

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 5:25 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC