简体   繁体   中英

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

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>";

 ?> 

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM