SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    sitepoint forums
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach and checkbox error

    hi,
    When i try to insert multiple data from a form using checkbox. What happens is it only insert the last data only. If the user checks lets say ten boxes, it inserts the last data only in all those ten boxes. It doesn't loop. Please help to fix this. thanks

    PHP Code:
    //FORM
    echo "<form method=\"POST\" action=\"form.php\">";
    while (
    $myrow mysql_fetch_array($sql)) {
             echo 
    "<input type=\"checkbox\" name=\"records_list[]\" value=\"$myrow[ID]\">"
            echo 
    "<input type=\"hidden\" name=\"sid\" value=\"$myrow[ID]\">";
            echo 
    "<input type=\"hidden\" name=\"sdate\" value=\"$myrow[DATE]\">";

    echo 
    "<input type=\"submit\" value=\"submit\" name=\"submit\">";
    echo 
    "</form>" 



    PHP Code:
    //Process the form
    if ($submit) {
        foreach (
    $_POST['records_list'] as $records_id) {
        
    $sql mysql_query("INSERT INTO TABLE (S_ID,S_DATE) values ('$sid','$sdate')"); 
        }


  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // FORM
    echo '<form method="post" action="form.php">';
    $i 0;
    while (
    $myrow mysql_fetch_array($sql)) {
            echo 
    '<input type="checkbox" name="records_list[]" value="'.$i.'">';
            echo 
    '<input type="hidden" name="sid['.$i.']" value="'.$myrow[ID].'">';
            echo 
    '<input type="hidden" name="sdate['.$i.']" value="'.$myrow[DATE].'">';
            
    $i++;
    }
    echo 
    '<input type="submit" value="Submit" name="submit">';
    echo 
    '</form>'
    PHP Code:
    // Process the form
    if ($_POST['submit']) {
        for (
    $i 0$c count($_POST['records_list']); $i $c$i++) {
            
    $id    = (int)$_POST['records_list'][$i];
            
    $sid   = (get_magic_quotes_gpc()) ? $_POST['sid'][$id]   : mysql_escape_string($_POST['sid'][$id]);
            
    $sdate = (get_magic_quotes_gpc()) ? $_POST['sdate'][$id] : mysql_escape_string($_POST['sdate'][$id]);
            
            
    $sql "INSERT INTO TABLE (S_ID, S_DATE) VALUES ('$sid','$sdate')";
            
    mysql_query($sql) or die(mysql_error());
        }


  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Denmark
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this
    PHP Code:

    $records_list 
    $_POST['records_list'];

      if (
    $records_list == ''$records_list = array();


    foreach (
    records_list as $records_id) {

    $sql = @mysql_query("INSERT INTO TABLE SET 

    S_ID='
    $sid',
    S_DATE='sdate'

    "
    );




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
  •