mardi 5 mai 2015

Fetching Multiple Rows From a Table, change fields data And Adding Them Into Another Table

I need to fetch Multiple rows from a table, change some fields data and then by check box selection I need to add selected rows to another table. I have seen a complete working code which is posted by Logan Wayne in the URL: How to redirect users to login page if they haven't logged in?. but thing is i need to change some fields value before copy it to another table. but so far i am unable to achieve it. Need your advice on this. below is what i tried so far.

<?php
  
$con=mysqli_connect("localhost","root","","Test"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */

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

}

$result_set = mysqli_query($con,"SELECT * FROM wm_order_entry");  

$num_messages = mysqli_num_rows($result_set);   

echo "<table border='1' align='center'>";

echo "<tr><th>PI_No</th><td>Parent_Name</td><td>Parent_Contact_Details</td><td>Code</td><td>Quantity</td></tr>";      
   
$num=0; /* THIS WILL BE SET AS YOUR COUNTER */

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

$id = $row['id'];

  $PI_No = $row['PI_No'];

  $Parent_Name = $row['Parent_Name'];                             
   
  $Parent_Contact_Details = $row['Parent_Contact_Details'];
   
  $Code = $row['Code'];                             

  $Qty = $row['Qty']; 
   
  $Creation_Date = $row['Creation_Date'];
   
  $num++;  /* INCREMENT NUM */

  echo "<form action='confirm_mem.php' method='POST'>";
   
  echo "<tr align='center'><th>$PI_No</th>";                  

  echo "<td><input type='text' name='$Parent_Name'></td>";                   

  echo "<td><input type='text' name='$Parent_Contact_Details'></td>";

  echo "<td><input type='text' name='$Code'></td>";                   

  echo "<td>$Qty</td>";

   echo "<td>$Creation_Date</td>";

   echo "<td><input type='checkbox' name='member[$num]' value='$id'></td></tr>";


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

?>

[b]confirm_mem.php[/b]

<html>

<body>

<?php

 $con=mysqli_connect("localhost","root","","Test"); /* 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'];

header("LOCATION:form.php");  /* 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 {

$result=mysqli_query($con,"SELECT * FROM wm_order_entry WHERE id='$member[$x]'");

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

$PI_No = $row['PI_No'];

$Parent_Name = $row['Parent_Name'];                             

$Parent_Contact_Details = $row['Parent_Contact_Details']; 

$Code = $row['Code'];

$Qty=$row['Qty']; 
 
}

mysqli_query($con,"INSERT INTO commercial_invoice (PI_No, Parent_Name, Parent_Contact_Details,Code,Qty)

VALUES ('$PI_No','$Parent_Name','$Parent_Contact_Details','$Code','$Qty')");

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

}
 
?>

</body>
 
</html>



Aucun commentaire:

Enregistrer un commentaire