Weird PHP Problem with Checkboxes - *Hard*

Discussion in 'PHP' started by Pixel T., Apr 19, 2010.

  1. #1
    Hey guys,

    I'm stuck in a weird situation. Heres what I have going on with my script. Please bare with me as this is really hard to explain.

    I created a table for username, password, and shop for "Shop Owners". I then created another table for username and password for "Normal Users". But when I'm registering for a shop owner, I added a code that would add an extra row for every new shop to "Normal users" .

    When filling out the registration form for "Normal Users", they have to click on the checkboxes of the shops they have shopped at. To get all the latest shops, I run this PHP code.

    
    
    <?php 
    
    $result = mysql_query("SELECT * FROM shopowners");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<input type='checkbox' name='" . $row['Shop'] . "' value='1' /> - " . $row['Shop'];
      echo "<br />";
      }
    
    ?> 
    
    
    PHP:
    Thats all well and nice but my problem is how would I go about inserting the values in the checkboxes to the "Normal Users" database. Because before, when creating the shop owners accounts, I made it so that everytime a new shop it added, it adds a row to the "Normal Users".

    Heres what I tried but it didn't work.

         	$registerquerytwo = mysql_query("INSERT INTO normalusers (Username, Password, Name
    
    $result = mysql_query("SELECT * FROM shopowners");
    
    while($row = mysql_fetch_array($result))
      {
      echo  "," . $row['Shop'];
      }
    
    ) VALUES ('".$usernametwo."', '".$passwordtwo."', '".$name."'
    
    $result = mysql_query("SELECT * FROM shopowners");
    
    while($row = mysql_fetch_array($result))
      {
      echo  ",\'\".$" . $row['Shop'] . ".\"\';
      }
    
    )");
    
    
    PHP:

    If anyone has a clue about what I'm trying to do here, please help me out. I'd greatly appreciate it.

    Thanks,
     
    Pixel T., Apr 19, 2010 IP
  2. job0107

    job0107 Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe you are trying to do something like this:
    
    $shopColumns = "";
    $shopValues = "";
    $result = mysql_query("SELECT Shop FROM shopowners");
    while($row = mysql_fetch_assoc($result))
    {
     $shopColumns .= $row['Shop'].",";
     $shopValues .= "'".($_POST[$row['Shop']]=="1"?$_POST[$row['Shop']]:0)."',";
     }
    $shopColumns = substr($shopColumns,0,strlen($shopColumns)-1); // Remove last comma from $shopColumns. //
    $shopValues = substr($shopValues,0,strlen($shopValues)-1); // Remove last comma from $shopValues. //
    
    $registerquerytwo = mysql_query("INSERT INTO normalusers (Username, Password, Name, $shopColumns) VALUES ('$usernametwo', '$passwordtwo', '$name', $shopValues)");
    
    PHP:
     
    job0107, Apr 19, 2010 IP
    Pixel T. likes this.
  3. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #3
    Thanks a lot bro. That exactly what I was looking for. +Rep for you :)
     
    Pixel T., Apr 20, 2010 IP
  4. job0107

    job0107 Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Your welcome.
     
    job0107, Apr 20, 2010 IP