Re: Foreach and mydql_query problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It works now and it was a php thing.When I combine a for and while loop
together with Limit in my query it works.
Karl

mandag 22. juli 2013 skrev Liam følgende:

> Shouldn't $_POST['number_of_itemsi'] be $_POST['number_of_items']
>
>
> Kind Regards,
>
> Liam.
> 3Sharp Ltd.
>
> T: 0845 6018370
> F: 0845 6018369
>
> -----Original Message-----
> From: Karl-Arne Gjersøyen [mailto:karlarneg@xxxxxxxxx <javascript:;>]
> Sent: 22 July 2013 12:39
> To: PHP Mailinglist
> Subject: Re:  Foreach and mydql_query problem
>
> 2013/7/22 Tamara Temple <tamouse.lists@xxxxxxxxx <javascript:;>>
>
> >
> > On Jul 22, 2013, at 1:19 AM, Karl-Arne Gjersøyen <karlarneg@xxxxxxxxx<javascript:;>
> >
> > wrote:
> >
> > > Hello again.
> > > I have this this source code that not work as I want...
> > >
> > > THe PHP/HTHML form fields is generated by a while loop and looks like
> > this:
> > >
> > > <input type="number" name="number_of_items[]" size="6" value="<?hp
> > > echo "$item"; ?>" required="required">
> > >
> > >
> > > the php source code look like this:
> > > <?php
> > > if(!empty($_POST['number_of_itemsi'])){
> > >    include('../../connect.php');
> > >
> > >        foreach($number_of_items as $itemi){
> > >        echo "$itemi<br>";
> > >
> > >            $sql = "UPDATE item_table SET number_item = '$item' WHERE
> date
> > > = '$todays_date' AND sign = '$username'";
> > >            mysql_query($sql,$connect) or die(mysql_error());
> > >     }
> > > }
> > >
> > > ?>
> > >
> > > The problem is:
> > > Foreach list every items as expected in PHP doc and I thought that $sql
> > and
> > > mysql_query should be run five times when I have five items.
> > > But the problem is that only the very last number_of_items is written
> > when
> > > update the form..
> > > I believe this is becayse number_of_items = '$item in $sqk override
> every
> > > earlier result.
> > >
> > > So my querstion is. How to to update the database in this case?
> > >
> > > Thanks again for your good advice  and time to help me.
> > >
> > > Karl'
> >
> > Either the code you posted isn't the actual code, or if it is, the errors
> > should be rather obvious. Post *actual* code.
> >
> >
>
> // The acutual source code is below:
> // ==============================================
> if(!empty($_POST['antall_kolli'])){
>     include('../../tilkobling.php');
>
>         foreach($antall_kolli as $kolli){
>         echo "$kolli<br>";
>
>             //    echo $kolli. "<br>";
>             $sql = "UPDATE transportdokument SET antall_kolli_stk =
> '$kolli' WHERE dato = '$dagens_dato' AND signatur = '$brukernavn'";
>             mysql_query($sql,$tilkobling) or die(mysql_error());
>      }
> }
>
> // THE PHP/HTML Form below:
> include('../../tilkobling.php');
>
>     $sql = "SELECT * FROM transportdokument WHERE dato = '$dagens_dato' AND
> signatur = '$brukernavn'";
>     $resultat = mysql_query($sql, $tilkobling) or die(mysql_error());
>     while($rad = mysql_fetch_array($resultat, MYSQL_ASSOC)){
>         $valgt_lager = $rad['valgt_lager'];
>         $un_nr = $rad['un_nr'];
>         $sprengstofftype = $rad['sprengstofftype'];
>         $varenavn = $rad['varenavn'];
>         $varenr = $rad['varenr'];
>         $antall_kolli = $rad['antall_kolli_stk'];
>         $adr_vekt_kg = $rad['adr_vekt_kg'];
>         $varenavn = $rad['varenavn'];
>         $emb = $rad['emb'];
> ?>
> <tr>
>     <td align="left" valign="top"><?php echo "$un_nr"; ?></td>
>     <td align="left" valign="top"><strong>Sprengstoff</strong>,<?php echo
> "$sprengstofftype"; ?></td>
>     <td align="left" valign="top"><?php echo "$varenavn"; ?></td>
>     <td align="left" valign="top"><strong>1.1D</strong></td>
>     <td align="left" valign="top">&nbsp;</td>
>     <td align="left" valign="top"><?php echo "$emb"; ?></td>
>     <td align="left" valign="top">
>     <input type="hidden" name="varenr[]" value="<?php echo "$varenr"; ?>">
>     <input type="number" name="antall_kolli[]" size="6" value="<?php echo
> "$antall_kolli"; ?>" required="required">
>     </td>
>     <td align="left" valign="top"><input type="text" name="exan_kg_ut[]"
> size="6" value="<?php echo "$adr_vekt_kg"; ?>" required="required"></td>
> </tr>
> <?php
> $total_mengde_kg_adr += $rad['adr_vekt_kg'];
> $total_antall_kolli += $rad['antall_kolli_stk'];
>     }
>
> ?>
> include('../../tilkobling.php');
>
>     $sql = "SELECT * FROM transportdokument WHERE dato = '$dagens_dato' AND
> signatur = '$brukernavn'";
>     $resultat = mysql_query($sql, $tilkobling) or die(mysql_error());
>     while($rad = mysql_fetch_array($resultat, MYSQL_ASSOC)){
>         $valgt_lager = $rad['valgt_lager'];
>         $un_nr = $rad['un_nr'];
>         $sprengstofftype = $rad['sprengstofftype'];
>         $varenavn = $rad['varenavn'];
>         $varenr = $rad['varenr'];
>         $antall_kolli = $rad['antall_kolli_stk'];
>         $adr_vekt_kg = $rad['adr_vekt_kg'];
>         $varenavn = $rad['varenavn'];
>         $emb = $rad['emb'];
> ?>
> <tr>
>     <td align="left" valign="top"><?php echo "$un_nr"; ?></td>
>     <td align="left" valign="top"><strong>Sprengstoff</strong>,<?php echo
> "$sprengstofftype"; ?></td>
>     <td align="left" valign="top"><?php echo "$varenavn"; ?></td>
>     <td align="left" valign="top"><strong>1.1D</strong></td>
>     <td align="left" valign="top">&nbsp;</td>
>     <td align="left" valign="top"><?php echo "$emb"; ?></td>
>     <td align="left" valign="top">
>     <input type="hidden" name="varenr[]" value="<?php echo "$varenr"; ?>">
>     <input type="number" name="antall_kolli[]" size="6" value="<?php echo
> "$antall_kolli"; ?>" required="required">
>     </td>
>     <td align="left" valign="top"><input type="text" name="exan_kg_ut[]"
> size="6" value="<?php echo "$adr_vekt_kg"; ?>" required="required"></td>
> </tr>
> <?php
> $total_mengde_kg_adr += $rad['adr_vekt_kg'];
> $total_antall_kolli += $rad['antall_kolli_stk'];
>     }
>
> ?>
>


-- 
Hjemmeside: http://www.karl-arne.name/

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux