php-general Digest 22 Jul 2013 08:03:50 -0000 Issue 8306

Topics (messages 321666 through 321668):

Foreach and mydql_query problem
        321666 by: Karl-Arne Gjersøyen
        321667 by: Tamara Temple
        321668 by: Stuart Dallas

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
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'

--- End Message ---
--- Begin Message ---
On Jul 22, 2013, at 1:19 AM, Karl-Arne Gjersøyen <karlar...@gmail.com> 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.


--- End Message ---
--- Begin Message ---
On 22 Jul 2013, at 08:04, Tamara Temple <tamouse.li...@gmail.com> wrote:

> On Jul 22, 2013, at 1:19 AM, Karl-Arne Gjersøyen <karlar...@gmail.com> 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 error is rather obvious: it loops around an array running an update 
statement that will modify a single row in the table, so it's not surprising 
that it appears like only the last entry in the array has been stored.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---

Reply via email to