Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts

    update db with each item in a loop

    Hi to everyone, this is my first post here and comes out of pure frustration and desperation :-|

    I am trying to retreive all the items from a multi lined textarea on a form, then after selecting each one run a db update to that item in the db.
    Sounds pretty straightforward to me ....
    So far I am able to echo each item from my exploded list and they all show correctly.

    Now the bit I am stuck with is getting each item in turn updated in the db.
    It will only update the last item in the list no matter what I do.

    I have tried while, foreach, do while, for, etc but no matter what I try I cannot get it to update more than the last one.....

    I am presuming that my update statement cannot execute due to each item not being seperated enough so it is just selecting the last in the list.
    If there is only one item then it works just fine and the update executes.
    more than one and it just updates the last in the list!

    If any of you people out there in the ether can point me in the right direction I would be eternally greatful.
    Many thanks
    clunky ;-)
    Last edited by clunky; 07-12-2007 at 12:56 PM. Reason: maybe incorrect title

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Let me get this straight you have a textarea that you need to seperate each line of text and sort into an array.

    PHP Code:
      <?PHP
           $text
    =$_GET['name_of_textarea'];
           
    $txt_array=explode (chr(13),$text);

           foreach (
    $txt_array as $this_txt)
           {
    //P.S im guessing sql table/entity/field names
                
    mysql_query('insert into table (text) values ("'.$this_txt.'")');
            }
       
    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by timgolding View Post
    Let me get this straight you have a textarea that you need to seperate each line of text and sort into an array.

    PHP Code:
      <?PHP
           $text
    =$_GET['name_of_textarea'];
           
    $txt_array=explode (chr(13),$text);

           foreach (
    $txt_array as $this_txt)
           {
    //P.S im guessing sql table/entity/field names
                
    mysql_query('insert into table (text) values ("'.$this_txt.'")');
            }
       
    ?>
    Hi Tim
    Many thanks for your reply.

    I am using a textarea called stock_in.
    I can get it to produce an echo on the results page after the form submission, like so....
    FOGG312 BLK26
    FOGG805 BEI26
    FOGG311 BLK26

    each of these is a scanned in barcode which corresponds to items in stock.
    I want to update another field in the row relative to the code.
    ie. mysql_query("UPDATE " . PRODUCTS . " SET products_store='instock' where products_model='$a'");
    $a would be any of the scanned in barcodes.
    currently using the above example results it will only update FOGG311 BLK26

    So I am presuming your solution would be something like this using my parameters....
    PHP Code:
      <?PHP
           $text
    =$_GET[stock_in];
           
    $txt_array=explode (chr(13),$text);

           foreach (
    $txt_array as $this_txt)
           {
    mysql_query("UPDATE " PRODUCTS  " SET products_store='instock' where products_model='$this_txt");
            }
       
    ?>
    How does that sound?

    OK an update....
    It doesn't work :-(

    to get the correct code format (which has spaces) I have used this...
    PHP Code:
    $txt_array=explode("\n"$text); 
    It produces the correct codes with
    PHP Code:
    echo "$this_txt...<br>"
    FOGG312 BLK26 ...
    FOGG311 BLK26 ...
    FOGG805 BEI26...

    It still updates just the last item in the list.
    Last edited by clunky; 07-11-2007 at 04:49 PM.

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Loop Help ? Please? Someone?

    This seems absurd.
    Somewhere within some kind of loop, whether it be a for, while, foreach or whatever it must be possible to update a db and then move to the next item and update that.

    why does this only update the db with the last item in the list?

    PHP Code:
    my multi line list produced by the form POST is ...
    FOGG312    BLK26
    FOGG805    BEI26
    FOGG311    BLK26

    /////////PHP CODE////////
    $prod $_REQUEST[stock_in];//input from form
    $wordChunks explode("\n"$prod);//explode at the end of item name
    foreach ($wordChunks as $newitems//creates each new item name
    {
    echo 
    "$newitems<br>";// prints out correctly as above, just to make sure
    mysql_query("UPDATE " PRODUCTS  " SET products_store='some value' where products_model ='$newitems'"); //update the db
           

    How do I get the first items updated before the last one?
    Or indeed how to update all of them in the list?
    Why are the first two ignored and just the last processed?
    Please help a poor lost soul :-(
    Cheers

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You should be using or die(mysql_error()) on your mysql_query() statement because I'm guessing its likely returning an error because its trying to update an empty table.
    PHP Code:
    foreach ($wordChunks as $newitems//creates each new item name
    {
    echo 
    "$newitems<br>";// prints out correctly as above, just to make sure
    $sql "UPDATE PRODUCTS SET products_store='some value' where products_model='$newitems'";
    mysql_query($sql) or die('Error: '.mysql_error().'<br>The query was:'.$sql.'<br>'); //update the db

    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi
    The database table is not empty, the update only effects the last item in the string.
    Lets say there are 4 items in the list, it ignores the first 3???
    Its really wierd and I have now spent 2 days trying to work this out, driving me nuts......


    And no or die(mysql_error()) errors produced

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Then where are you getting PRODUCTS from? Its not a variable so why are you trying to concatenate it? Did you even try my suggestion? I think you should. You are asking for help not us.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Then where are you getting PRODUCTS from? Its not a variable so why are you trying to concatenate it? Did you even try my suggestion? I think you should. You are asking for help not us.
    products is the database table (not PRODUCTS)
    I didn't think I was trying to concatenate it? Where am I doing that?

    All I am trying to do is use the items from the posted form and update the db with an new value using the item code in the where clause.

    It does work to a degree by updating the last item in the list.
    But it ignores the rest before it.
    Here is the current script
    PHP Code:
    $prod $_REQUEST[stock_in];//input from form 
    $wordChunks explode("\n"$prod);//explode at the end of item name /
    for($i 0$i count($wordChunks);$i++){
    $sql "UPDATE products SET products_store='some value' where products_model='$wordChunks[$i]'";
    mysql_query($sql) or die('Error: '.mysql_error().'<br>The query was:'.$sql.'<br>'); //update the db

    I am desperatley asking for help :-)
    Last edited by clunky; 07-12-2007 at 01:12 PM.

  • #9
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by clunky View Post
    products is the database table (not PRODUCTS)
    I didn't think I was trying to concatenate it? Where am I doing that?
    PHP Code:
    mysql_query("UPDATE " PRODUCTS  " SET products_store='some value' where products_model ='$newitems'"); //update the db 
    Right here.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #10
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by StupidRalph View Post
    PHP Code:
    mysql_query("UPDATE " PRODUCTS  " SET products_store='some value' where products_model ='$newitems'"); //update the db 
    Right here.
    Hi That was from an earlier helpful suggestion I tried.
    I have edited my earlier post above with the current code, it doesnt make any difference, neither work.

    This would appear to be a really simple task, get the form data, seperate each bit, then use it as the where clause for an update of another field.

    :-|

  • #11
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by clunky View Post
    Hi That was from an earlier helpful suggestion I tried.
    I have edited my earlier post above with the current code, it doesnt make any difference, neither work.

    This would appear to be a really simple task, get the form data, seperate each bit, then use it as the where clause for an update of another field.

    :-|
    PHP Code:
    $prod $_REQUEST[stock_in];//input from form 
    $wordChunks explode("\n"$prod);//explode at the end of item name /
    for($i 0$i count($wordChunks);$i++){
    echo 
    "$wordChunks[$i] ...<br>";
    $sql "UPDATE products SET products_store='some value' where products_model='$wordChunks[$i]'";
    mysql_query($sql) or die('Error: '.mysql_error().'<br>The query was:'.$sql.'<br>'); //update the db

    this is the current script, I have just added the echo to see what its getting...
    FOSS312 BLK26
    FOSS805 BEI26
    FOSS311 BLK26

    It ran the script but only updated
    products_store where products_model=FOSS311 BLK26

    I would really like some help here please
    Last edited by clunky; 07-12-2007 at 01:23 PM.

  • #12
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Well since you know its running one query successfully how about constructing your update string to look like the following

    Code:
    UPDATE products SET products_store='some value' WHERE products_model IN ('FOSS312 BLK26', 'FOSS805 BEI26', 'FOSS311 BLK26')
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #13
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by StupidRalph View Post
    Well since you know its running one query successfully how about constructing your update string to look like the following

    Code:
    UPDATE products SET products_store='some value' WHERE products_model IN ('FOSS312 BLK26', 'FOSS805 BEI26', 'FOSS311 BLK26')
    That sounds very cool;-)
    As I am not brill with arrays etc how would I get that in there?
    Like using implode to rebuild the string?

  • #14
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    One second let me code it.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #15
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    $wordChunks = array('FOSS312 BLK26''FOSS805 BEI26''FOSS311 BLK26');
    $wordChunks implode("','"$wordChunks);
    //echo $wordChunks;

    $sql "UPDATE products SET products_store='some value' WHERE products_model IN ('" .$wordChunks."');";
    echo 
    $sql
    I simply used implode() to add the comma and single quotes. I hardcoded the outer single quotes. You should be able how to do it from here.

    Edit: LoL I didn't see that you seen to use the implode(). I'm taking it that you've gotten it too.
    Last edited by StupidRalph; 07-12-2007 at 01:57 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •