SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Write SQL array for multiple checkbox values

    I found a simple code to pass the values of each checkbox and pass their values onto the next page. Now that the checked values are passed to the next page, how do I write these new checkbox values into the table for each returned value?

    Here is the code that prints the form and checkbox and also passes it on to the next page:

    Code:
    <form name='form1' method='post' action='editsubscriber3.php'>
    
    <?
    dbConnect();		
    // This gets all of the reports so that the admin can set permissions for individual reports
    $sql = "SELECT * FROM emt_report ORDER BY date_year DESC, date_month DESC, company ASC";   
    $result = mysql_query($sql) or user_error("mysql error " . mysql_error());
    $rank = 1;
    
    while($row = mysql_fetch_assoc($result)) 
    {
        $id = $row['id'];
    
       print("<input name='$id' type='checkbox' value='$id'>"); 
    
      $rank++;
    }
    Here is the second page which pulls each checked value and prints it on the page. What I need to do instead of print is to write to the database the user id and checkbox value for every checkbox. How do I write an SQL array to insert a new row for each checked value? Here is the code for the second page:
    Code:
    foreach ($_POST as $key => $value ) 
    // --- HOW DO I PRINT AN SQL STATEMENT HERE TO WRITE A NEW ROW FOR EACH RETURNED CHECKBOX VALUE? ---
    echo "$key <br>";

    How do I write the checkbox values into the database? Do I use an array to write an SQL statement for each result? Suggestions? I know it needs to look something like this, but executed each time there is a returned value:

    $sql = "INSERT into user_reports VALUES ('$user' , '$key')";

  2. #2
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out mysql_query(). And, you should escape use input with mysql_real_escape_string().


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
  •