7 Replies - 773 Views - Last Post: 13 May 2015 - 10:11 PM Rate Topic: -----

#1 ripon_81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-October 14

Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 05 May 2015 - 08:04 PM

Hi,

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. So far I am able to achieve copying selected rows values from a table and adding them to another table. But failed to achieve change the selected rows fields data. Below is the code what i have tried so far. Any advise will be highly appreciated.

[b]form.php[/b]

<?php

/* ESTABLISH THE CONNECTION */

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

/* 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)){       
$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>"; 

/* 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 $PI_No 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>";

?> 


[b]confirm_mem.php[/b]

<html>
<body>

<?php

/* ESTABLISH THE CONNECTION */

$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 {

/* GET THE DATA FROM MEMBERSNEEDREG TABLE */
$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'];  
}

/* THEN INSERT THE FETCHED DATA TO MEMBERS TABLE */
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')");

} /* 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>




Is This A Good Question/Topic? 0
  • +

Replies To: Fetching Multiple Rows from Table and Adding Them into Another Table

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 05 May 2015 - 08:31 PM

You really should be using prepared statements, as you've been told before.
Was This Post Helpful? 1
  • +
  • -

#3 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2040
  • View blog
  • Posts: 6,249
  • Joined: 15-January 14

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 06 May 2015 - 08:55 AM

You're putting the data from the database in the inputs' name attribute instead of the value attribute. Give them names similar to the checkboxes so you can loop through them.
Was This Post Helpful? 0
  • +
  • -

#4 ripon_81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-October 14

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 12 May 2015 - 04:38 AM

I can now pass the value from html page to database by check box selection. But problem i am facing now is rows are not inserted as per my selection. only (PI_NO) column is correctly inserted as per row selection. other fields value are inserting sequence wise. for example: if i select rows PI_No-34,35... Customer: XYZ, Address:TTT and after inserting i am getting selected PI_NO (34,35..), and 1st rows customer name(1,2... row customer of PI table) and Address(1,2... row customer address of pi table). below is my code what i have done so far:

<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("Test",$db);
$qry="select * from  pi";
$result=mysql_query($qry);
?>
<form action="check.php"   method="post">
<table>
<tr><th>A</th>
<th>B</th>
<th>C</th>
</tr>
  <?php
while($row=mysql_fetch_array($result))
{
  ?>
    <tr>
      <td>
        <input type="checkbox" name="name[]" value="<?php echo $row['id']; ?>">
        <?php echo $row["PI_NO"]; ?>
        <!-- HERE IS THE START OF YOUR TWO HIDDEN INPUT -->
        <input type="hidden" name="piaddress[]" value="<?php echo $row["PI_ADDRESS"]; ?>">
        <input type="hidden" name="customer[]" value="<?php echo $row["CUSTOMER_NAME"]; ?>">
      </td>
      <td><?php echo $row['CUSTOMER_NAME']; ?></td>
      <td><?php echo $row['PI_ADDRESS']; ?></td>
    </tr>
  <?php
} /* END OF WHILE LOOP */
?>
<input type="submit" value="save" id="submit">

</table>
</form>



<?php

  $db=mysql_connect("localhost","root","");
  mysql_select_db("Test",$db);

  $counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */

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

    if(!empty($_POST["name"][$x])){
      $PI_NO = mysql_real_escape_string($_POST["name"][$x]);
      $CUSTOMER_NAME = mysql_real_escape_string($_POST["customer"][$x]);
      $PI_ADDRESS = mysql_real_escape_string($_POST["piaddress"][$x]);

      $qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
      mysql_query($qry);

    } /* END OF CHECKING THE CHECKBOX IF SELECTED */

  } /* END OF FOR LOOP */
header("location:form.php");
?>


This post has been edited by CTphpnwb: 12 May 2015 - 11:03 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 12 May 2015 - 11:04 AM

I stop reading when I see this: mysql_connect
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,936
  • Joined: 10-May 07

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 12 May 2015 - 11:12 AM

That's fine, they stop responding when they read 'Prepared'.
Was This Post Helpful? 1
  • +
  • -

#7 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2040
  • View blog
  • Posts: 6,249
  • Joined: 15-January 14

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 12 May 2015 - 11:22 AM

Yeah, what happened to your code? You were at least using the mysqli extension. Why did you downgrade to mysql?
Was This Post Helpful? 0
  • +
  • -

#8 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: Fetching Multiple Rows from Table and Adding Them into Another Table

Posted 13 May 2015 - 10:11 PM

Not to mention SQL injection. What happens when the customer name is changed to ',''),('',' in the HTML? Just because the field is hidden doesn't mean it can't be altered by someone in their browser.

But as to your problem, on the postback, have you tried the following in your code?
error_log(var_export($_POST, true));


...and then checking your error logs? You need to look at your $_POST array to see if the problem if with the data being posted back, or the way you are recursing through the data.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1