Results 1 to 6 of 6

Thread: Updating Multiple Rows in a Database

  1. #1
    Join Date
    Jul 2009
    Location
    London
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Updating Multiple Rows in a Database

    I am working on some php that will post the date an email is sent to a database. Each recipient of that email must have their date updated.
    The first string should select an array of sub_id (subscriber id) from a data table for messages.

    This is what I have so far:
    PHP Code:
    $mysub=array("SELECT sub_id FROM mail_sub_msgs WHERE msg_id = "$mrecord['msg_id']); 
    foreach (
    $mysub as $value
    var_dump($value); 
    $ldatabase->execute_query("UPDATE mail_subscribers SET last_sent=now() WHERE sub_id = ($mysub[0])"); 
    Which gives message:
    string(58) "SELECT sub_id FROM mail_sub_msgs WHERE msg_id = 244" }

    This only works for one email address but if there is more than one, nothing is updated!

    I don't see why it is not printing a list of values for multiple sub_id.
    Is there not something I have to do to run the string & SELECT the values before the var_dump()...

    Any help is appreciated, this is my first week with php.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    $myvar[0] will only return one result. Use $value instead.

    PHP Code:
    $ldatabase->execute_query("UPDATE mail_subscribers SET last_sent=now() WHERE sub_id = ($value)"); 
    - Josh

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ...Ive just tried your recommendation but it has not made a change.

    I can update one at a time but if I send two emails no updates are made.

    var_dump message:
    string(58) "SELECT sub_id FROM mailmachine_sub_msgs WHERE msg_id = 247"

  4. #4
    Join Date
    Jul 2009
    Location
    London
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Got it, thanks!
    ...needed an IN...

    PHP Code:
    $end=date("F j, Y, g:i a");
    $ldatabase->execute_query("UPDATE mail_newsletter SET sent='Y',date_sent=now() WHERE msg_id = "$mrecord['msg_id']);
    $mysub=array("SELECT sub_id FROM mail_sub_msgs WHERE msg_id = "$mrecord['msg_id']);
    foreach (
    $mysub as $value)
    $ldatabase->execute_query("UPDATE mail_subscribers SET last_sent=now() WHERE sub_id IN ($value)"); 

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    PHP Code:
    $mysub=array("SELECT sub_id FROM mail_sub_msgs WHERE msg_id = "$mrecord['msg_id']); 
    I am curious as to why you are assigning this return to an array?
    If the database was set up correctly, the return will be either 1 value or an empty set.

    Try
    PHP Code:
    $mysubsprintf("SELECT sub_id FROM mail_sub_msgs WHERE msg_id = %d"$mrecord['msg_id']);

    $ldatabase->execute_query("UPDATE mail_subscribers SET last_sent=NOW() WHERE sub_id ="{$value}"); 
    Note: %d specifies an integer. For more information, review sprintf


    There is no reason you should be assigning the

  6. #6
    Join Date
    Jul 2009
    Location
    London
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ...but surely if I send, say 10 emails, then $mysub is an array of 10 sub_id?

    If I limit it to one value, only one sub_id gets updated ...which was the problem I had in the first place.
    Maybe you are suggesting I arrange some sort of loop but that just seems more complicated?

Bookmarks

Posting Permissions

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