SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Update Query in a forloop

    Hi all

    I have some problem with my code there is no errors with my code its for a game swelect list for user whos logged in to display wat games they like

    what i have is a list menu which is called product[]

    In my Actual PHP Code

    PHP Code:
    if(isset($_POST['setgames']))
                                                {
                                                    
    $product=$_POST['product'];
                                                    if(
    $product || $User)
                                                    {
                                                        
    //query to insert into cutomer
                                                        
    foreach($product as $prod)
                                                        {
                                                            
    $oQuery="update members set gamesplayed='".$prod."' WHERE username='".$_SESSION['user']."'";
                                                            echo 
    $oQuery;
                                                            
    $res2=$con->query($oQuery);
                                                            if(!
    $res2)
                                                            {
                                                                echo 
    "<br/><br/>Error Cannot Set Games Played Because &nbsp;".mysqli_error($con);
                                                            }
                                                        }
                                                    }
                                                } 
    its only doing

    update members set gamesplayed='Starcraft:Orginal' WHERE
    username='Admin'
    update members set gamesplayed='Starcraft:Brood' WHERE username='Admin'
    update members set gamesplayed='Diablo2' WHERE username='Admin'
    update members set gamesplayed='Diablo2:Lod' WHERE username='Admin'
    update members set gamesplayed='Warcraft' WHERE username='Admin'

    When its meant to be game 1,game2,game3,game4 not when they select more than one

    in the database for the field gamesplayed is varchar of 255

    What am i doing wrong

    can anyone help?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    it appears as though you want the gamesplayed column to end up with a comma-delimited list of game names, right?

    what you want is
    Code:
    UPDATE members 
       SET gamesplayed =
             CONCAT_WS(', '    
               , gamesplayed
               , 'Starcraft:Orginal' 
               , 'Starcraft:Brood' 
               , 'Diablo2' 
               , 'Diablo2:Lod' 
               , 'Warcraft' )
     WHERE username = 'Admin'
    however, if you are going to do anything besides displaying the contents of this field, such as searching for a particular value within this field, then i urge you to redesign your table to conform to first normal form, which does not allow comma-delimited values
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •