Jump to content

Inserting multiple records to a table using checkboxes


Tosey
 Share

Recommended Posts

Hi guys, please what am i doing wrong here? i have searched almost everywhere on the internet and found solutions similar to my own but they are not just working!! i am sorry if this question has been asked before. i'm not getting things right. the project is actually a very complex one. i have used many methods but all to no avail. Please help!

 I have two tables called "group_profile" and "group_members". The concept is that users have already created their groups. Then these groups have been stored into my database table. And now i want to retrieve the records from that table and display it on the "join_group.php" page so that that people can click to join those groups selected from the first table. Users can select multiple groups to join using checkboxes at a time. Therefore, when you join the group, the script will add both your data and the groups' to "group_members" table but if i select multiple groups and click on join button, it appears to add only the last group created in the "group_profile" table. Sometimes it will take me to the "processing-join-group.php" page and show me the script doesn't seem to work well. i'm sure there is something i'm doing wrong. Please here is my code so far:


This is the "join_group.php" page below:
<?php 

$other_following_account = mysqli_query($f->connectTo(), "SELECT * FROM group_profile");

if ($f->numR($other_following_account) > 0) {
    
    while($show_created_groups = $f->show_data($other_following_account)) {
    
        $select_member = $f->andFilterRecord("*", "hoollaa_group_members", "user_id", $_id, "group_id", $show_created_groups['id']);
        $chk_user_id = $f->show_data($select_member);
        $check_my_membership = $f->numR($select_member);
        
        if (!$check_my_membership) { 
        
         if ($show_created_groups['user_id'] != $_id) {
        ?>
            
              <div class="inner-main-cont-left main-cont-right box-shadow" style="height: 370px; width: 250px;">
    
     <?php 
      if ( empty($show_created_groups['group_photo2']) ) { ?>
     <span class="container">
          <span class="profile-avatar2"><?php echo substr($show_created_groups['group_name'], 0, 1);?></span>
        </span>    
    <?php    
      } else { ?>
          <img style="margin-bottom: -5px;" src="<?php echo $show_created_groups['group_photo2'];?>" />
    <?php
      }
    ?>
     <div class="profile-content" style="padding: 10px 8px 10px 10px; height: 80px;">
     <div class="main-cont-left-header-profile"><?php echo $show_created_groups['group_name'];?></div>
      <div class="clear-left"></div>
      <p style="word-wrap: break-word;">      
       <?php 
                 $d_string = $show_created_groups['group_description'];
                 $new_str = (strlen($d_string) > 63) ? trim(substr($d_string, 0, 60))."..." : $d_string;
                 echo $new_str;
                ?>
      
      </p> 
    </div>
     <input name="admin_id[]" type="" value="<?php echo $show_created_groups['user_id']; ?>" />
     <input name="group_name[]" type="" value="<?php echo $show_created_groups['group_name']; ?>" />
     <input name="group_admin[]" type="" value="<?php echo $show_created_groups['group_admin']; ?>" />
     <input name="group_id[]" type="" value="<?php echo $show_created_groups['id']; ?>" />

<input type="checkbox" name="join_data[]" value="<?php echo $show_created_groups['id']; ?>" id='c<?php echo $show_created_groups['id'];?>[]' class='chk-btn' />

     <label style="padding: 10px; margin: 60px 0px 0px 178px;" for='c<?php echo $show_created_groups['id'];?>[]'>Join</label>
    
    
    </div>
<?php    
        }
      }
    }
}
?>  

  <div class="clear-left"></div>
   
 </div>
        
        <button class="skip-btn floating-nav-btn btn btn__trigger btn__trigger--views" name="join_btn" type="submit">
         <i class="fa fa-caret-right" style="font-size: 32px;"></i>
        </button>
        
        </form>


And here is the page that is receiving the data from "join_group.php"

processing-join-group.php" page:


<?php 
 require_once "../set-header-dependencies.php";
/*
 $f->isUserLoggedIn_sess("auth", "../home.php");
 $f->isUserLoggedOut_sess("auth", "../index.php");
*/
 
 $time_zone = date_default_timezone_set("Africa/Lagos");
 $date_published = date("F d, Y");
 $time_published = date("H:i:sa");
 $time_stamp = time();

//Follow Selected group
if (isset($_POST['join_btn'])) {
if (isset($_POST['join_data'])) {
$display_follow_data = $f->post_data('join_data');
if (is_array($display_follow_data)) {   
foreach ($display_follow_data as $key => $array_id) {
    
$select_store = $f->andFilterRecord("*", "group_members", "user_id", $_id, "group_id", $array_id);    
$chk_user_id = $f->show_data($select_store);    
$select_from_main_store  = $f->filterRecord("*", "group_profile", "user_id", $array_id);    

while($use_selected_product = $f->show_data($select_from_main_store)) {

    $admin_id = $use_selected_product['user_id'];
    $group_name = $use_selected_product['group_name'];
    $group_admin = $use_selected_product['group_admin'];
    $group_id = $use_selected_product['id'];
    
if ($use_selected_product['id'] != $chk_user_id['group_id']) {
 $insert_follow_data = "INSERT INTO group_members(group_name,group_id,admin_id,group_admin,username,user_id,timestamp,date_published,time_published) VALUES ('$group_name','$group_id','$admin_id','$group_admin','$acc_username','$_id','$time_stamp','$date_published','$time_published')";
      $f->cts($insert_follow_data);
      $_SESSION['follow_success'] = "<script>$('body').tpPopup({avatar: 'hoollaa-icons/Notifications.png',username: '',message: 'Group joined.',openOnClick: 'join-groups.php'});</script>";
      $f->rdr("../home.php");
   } else {
  $_SESSION['follow_error'] = "<script>$('body').tpPopup({avatar: 'hoollaa-icons/Notifications.png',username: '',message: 'Could not add you to.',openOnClick: 'join-groups.php'});</script>";
   $f->rdr("../home.php");
    }
  }
  }
  }
 else {
$_SESSION['follow_error'] = "<script>$('body').tpPopup({avatar: 'hoollaa-icons/Notifications.png',username: '',message: 'Please select a group to join.',openOnClick: 'join-groups.php'});</script>";
   $f->rdr("../home.php");
  }
 } else {
$_SESSION['follow_error'] = "<script>$('body').tpPopup({avatar: 'hoollaa-icons/Notifications.png',username: '',message: 'Please select a group to join.',openOnClick: 'join-groups.php'});</script>";
   $f->rdr("../home.php");
  }
}

?>

Link to comment
Share on other sites

There seems to be some framework and data structures that are not described here, leaving parts of the code that I have no way to know what it's doing.

Try to reduce this to its most basic components. Which is the part of the code that's getting the POST data and putting it into the database?

Link to comment
Share on other sites

i am not sure where all these variables come from or their value ,but heres what you do :

when you load the page in the browser check if each the checkboxs have a different value  than the other .

when that is confirmed ,check the other page that receives the data from the form and try to echo the value of each checkbox to make sure the data was received properly .

one last thing was  $_SESSION['follow_success'] declared as an array ? if not, then it will only keep the last value received inside the loop .

you should also consider a different name for each checkbox, having multiple $_post . 

Godd luck .

Edited by john_jack
more details
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...