Fetching Multiple Rows from Table and Adding Them into Another Table and then Remove from Previous Table, Using Checkboxes

akirk541

In my administrator page, a list of people that wanted to register to the website will be shown.

Requests are inserted to my membersneedreg table. When a user has been accepted, user's row will then be move to the members table.

While fetching data from membersneedreg, I've added a checkbox on each row and decided to have $num variable as its value, which increments on every loop.

My problem is how will the system confirm that my checkbox is selected, wherein when submitted, the selected data row will then be move from membersneedreg table into members table?

Help will gladly be appreciated. If you have any questions or alterations needed on my post, don't hesitate to comment.

<?php


$result_set = mysql_query("SELECT * FROM membersneedreg");   
$num_messages = mysql_num_rows($result_set);    
// Create connection

echo "<table border=1 align='center'>";
echo "<tr><th>MemberID</th><td>FirstName</td><td>LastName</td><td>Date of birth</td><td>Email</td><td>Address</td><td>PostCode</td><td>UserName</td><td>Password</td><td>Annadale Relationship</td></tr> ";       


// Loop over all the posts and print them out
$num=0;
while( $row = mysql_fetch_assoc( $result_set ) ) {          
  $memberid1 = $row['MemberID']; 
  $fname1 = $row['FirstName'];                              
  $lastn1 = $row['LastName'];   
  $DOB1 = $row['Date_of_Birth']; 
  $Email1 = $row['Email'];
  $Address1 = $row['Address'];                              
  $Postcode1 = $row['PostCode'];   
  $UserName1 = $row['UserName']; 
  $Password1 = $row['Password'];
  $relationship1 = $row['Annadale_Relationship'];
  $num++;  //allows me to give each checkbox a different value.

  echo "<form action='confirm_mem.php'>";
  echo "<tr align='center'><th>$memberid1</th>";                   
  echo "<td>$fname1</td>";                    
  echo "<td>$lastn1</td>";
  echo "<td>$DOB1</td>";   
  echo "<td>$Email1</td>";    
  echo "<td>$Address1</td>";                    
  echo "<td>$Postcode1</td>";
  echo "<td>$UserName1</td>";
  echo "<td>$Password1</td>";   
  echo "<td>$relationship1</td>";
  echo "<td><input type='checkbox' name='member' value='$num'></td></tr>";                                     
 }
 echo "</table>";
 echo "<input type='submit' name='submit' value='Add Member'>";
 echo "</form>";

 ?> 
Logan Wayne

Lets first convert your MySQL to MySQLi. I'll put some explanations inside the comments /* */ as I move forward to the code.

IF you want to hear my advice, you should have just created 2 tables instead of 2 database. (Based on your explanation, you call membersneedreg and members as database).

Assume your membersneedreg and members are table, AND they both have the same columns.

You can save this, for example, as form.php:

<?php

/* ESTABLISH THE CONNECTION */

$con=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

$result_set = mysqli_query($con,"SELECT * FROM membersneedreg");   
$num_messages = mysqli_num_rows($result_set);    

echo "<table border='1' align='center'>";
echo "<tr><th>MemberID</th><td>FirstName</td><td>LastName</td><td>Date of birth</td><td>Email</td><td>Address</td><td>PostCode</td><td>UserName</td><td>Password</td><td>Annadale Relationship</td></tr>";       

/* FETCH THE DATA BASED ON THE QUERY RESULT_GET */

$num=0; /* THIS WILL BE SET AS YOUR COUNTER */

while($row = mysqli_fetch_array($result_set)){       

  $memberid1 = $row['MemberID']; 
  $fname1 = $row['FirstName'];                              
  $lastn1 = $row['LastName'];   
  $DOB1 = $row['Date_of_Birth']; 
  $Email1 = $row['Email'];
  $Address1 = $row['Address'];                              
  $Postcode1 = $row['PostCode'];   
  $UserName1 = $row['UserName']; 
  $Password1 = $row['Password'];
  $relationship1 = $row['Annadale_Relationship'];

  $num++;  /* INCREMENT NUM */

  echo "<form action='confirm_mem.php' method='POST'>";
  echo "<tr align='center'><th>$memberid1</th>";                   
  echo "<td>$fname1</td>";                    
  echo "<td>$lastn1</td>";
  echo "<td>$DOB1</td>";   
  echo "<td>$Email1</td>";    
  echo "<td>$Address1</td>";                    
  echo "<td>$Postcode1</td>";
  echo "<td>$UserName1</td>";
  echo "<td>$Password1</td>";   
  echo "<td>$relationship1</td>";
  echo "<td><input type='checkbox' name='member[$num]' value='$memberid1'></td></tr>"; 

/* THIS IS IMPORTANT, YOU SHOULD PUT TO AN ARRAY THE VALUE OF THIS CHECKBOX, AND THE VALUE WILL BE BASED ON THE FETCH ARRAY. IN MY EXAMPLE, THE $memberid1 VARIABLE */
  }

  echo "</table>";
  echo "<input type='hidden' name='hiddencounter' value='$num'>"; /* THIS IS FOR THE COUNTING PURPOSES */
  echo "<input type='submit' name='submit' value='Add Member'>";
  echo "</form>";

?> 

confirm_mem.php

<html>
<body>

<?php

/* ESTABLISH THE CONNECTION */

$con=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */

if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}

if(isset($_POST['submit'])){

$counter=$_POST['hiddencounter'];
$member=$_POST['member'];
echo $counter." submitted checkboxes"; /* TO CHECK HOW MANY CHECKBOX HAS BEEN GENERATED */

for($x=0;$x<=$counter;$x++){

if(empty($member[$x])){ /* IF MEMBER IS EMPTY, DO NOTHING */

}

else {

/* GET THE DATA FROM MEMBERSNEEDREG TABLE */
$result=mysqli_query($con,"SELECT * FROM membersneedreg WHERE MemberID='$member[$x]'");
while($row=mysqli_fetch_array($result)){
$memberid1 = $row['MemberID']; 
$fname1 = $row['FirstName'];                              
$lastn1 = $row['LastName'];   
$DOB1 = $row['Date_of_Birth']; 
$Email1 = $row['Email'];
$Address1 = $row['Address'];                              
$Postcode1 = $row['PostCode'];   
$UserName1 = $row['UserName']; 
$Password1 = $row['Password'];
$relationship1 = $row['Annadale_Relationship'];
}

/* THEN INSERT THE FETCHED DATA TO MEMBERS TABLE */
mysqli_query($con,"INSERT INTO members (MemberID, FirstName, LastName, Email, Address, PostCode, UserName, Password, Annadale_Relationship)
VALUES ('$memberid1','$fname1','$lastn1','DOB1','Email1','$address1','$Postcode1','$UserName1','$Password1','$relationship1')");

/* THEN DELETE FROM membersneedreg THE INSERTED DATA */
mysqli_query($con,"DELETE FROM membersneedreg WHERE MemberID='$member[$x]'"); 

} /* END OF ELSE */

} /* END OF FOR LOOP */

} /* END OF ISSET SUBMIT */

else {
header("LOCATION:form.php"); /* REDIRECT TO form.php IF DIRECTED TO THIS PAGE */
}

?>

</body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting multiple rows from a table

From Dev

Update multiple columns of a table using aggregate values from another table

From Dev

Subtract rows of one table from another table

From Dev

JQuery: How to drag multiple rows from one table to another?

From Dev

Fetching unique rows from one table that columns reference has not in another table

From Dev

select multiple column from one table and insert into another as rows

From Dev

Inserting rows in a table from another table using a third table

From Dev

Insert multiple rows from select into another table

From Dev

Update multiple rows in one table with differing values from another

From Dev

Field involving multiple rows from another table

From Dev

Insert multiple rows with single a query from one table into another in Oracle

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

Matlab adding rows to a table from another table with different dimensions

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Dev

Deleting multiple rows from a table

From Dev

Update multiple columns of a table using aggregate values from another table

From Dev

Select a particular record from a table using checkboxes

From Dev

Select rows from a table if one of them matches with another table

From Dev

Insert (multiple) new rows into a table from another table using a subquery?

From Dev

Remove a substring in a varchar field from multiple rows of a table

From Dev

Delete data from a table by fetching data from another table in mysql

From Dev

insert multiple rows mysql from another table

From Dev

How to remove the selected checkboxes from the table

From Dev

Adding multiple rows of a table to another table

From Dev

Copy rows from table to another

From Dev

Selecting multiple checkboxes from a table javascript

From Dev

How to add multiple rows of data from one sql table to another with selection based on checkboxes?

From Dev

Update MYSQL Table from multiple rows in another table

From Dev

Adding missing rows from the another table based on 2 columns

Related Related

  1. 1

    Deleting multiple rows from a table

  2. 2

    Update multiple columns of a table using aggregate values from another table

  3. 3

    Subtract rows of one table from another table

  4. 4

    JQuery: How to drag multiple rows from one table to another?

  5. 5

    Fetching unique rows from one table that columns reference has not in another table

  6. 6

    select multiple column from one table and insert into another as rows

  7. 7

    Inserting rows in a table from another table using a third table

  8. 8

    Insert multiple rows from select into another table

  9. 9

    Update multiple rows in one table with differing values from another

  10. 10

    Field involving multiple rows from another table

  11. 11

    Insert multiple rows with single a query from one table into another in Oracle

  12. 12

    MYSQL - UPDATE multiple rows from another table

  13. 13

    Matlab adding rows to a table from another table with different dimensions

  14. 14

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  15. 15

    Deleting multiple rows from a table

  16. 16

    Update multiple columns of a table using aggregate values from another table

  17. 17

    Select a particular record from a table using checkboxes

  18. 18

    Select rows from a table if one of them matches with another table

  19. 19

    Insert (multiple) new rows into a table from another table using a subquery?

  20. 20

    Remove a substring in a varchar field from multiple rows of a table

  21. 21

    Delete data from a table by fetching data from another table in mysql

  22. 22

    insert multiple rows mysql from another table

  23. 23

    How to remove the selected checkboxes from the table

  24. 24

    Adding multiple rows of a table to another table

  25. 25

    Copy rows from table to another

  26. 26

    Selecting multiple checkboxes from a table javascript

  27. 27

    How to add multiple rows of data from one sql table to another with selection based on checkboxes?

  28. 28

    Update MYSQL Table from multiple rows in another table

  29. 29

    Adding missing rows from the another table based on 2 columns

HotTag

Archive