从表中获取多行并将其添加到另一个表中,然后使用复选框从上一个表中删除

时间:2014-03-26 01:27:19

标签: php mysql checkbox

在我的管理员页面中,将显示想要注册到网站的人员列表。

请求被插入到我的membersneedreg表中。当用户被接受后,用户的行将移至members表。

membersneedreg获取数据时,我在每一行上添加了一个复选框,并决定将$num变量作为其值,该值在每个循环中递增

我的问题是系统将如何确认我的复选框已被选中,其中提交后,所选数据行将从membersneedreg表移至members表?

很乐意感谢您的帮助。如果您对我的帖子有任何疑问或改动,请不要犹豫。

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

 ?> 

1 个答案:

答案 0 :(得分:2)

让我们首先将 MySQL 转换为 MySQLi 。当我向前推进代码时,我会在评论 / * * / 中添加一些解释。

IF 您希望听到我的建议,您应该刚创建 2个表而不是 2个数据库。 (根据您的解释,您将membersneedregmembers称为数据库。

假设您的membersneedregmembers是表,并且它们都具有相同的列。

例如,您可以将其保存为 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>