0

I want to insert all my fetch data into another table using PHP, but I can't figure out how to do it.

I already fetched the selected data as you can see in image using check box

enter image description here

and I can fetch it. I don't know if fetch is the right word

enter image description here

Here is the code that I use:

<?php
require '../config/connect.php';
require '../config/config.php';


if(isset($_POST['save_multicheckbox']))
{
  foreach ($_POST['id'] as $id):
    $query=mysqli_query($conn,"SELECT * FROM `liveselling` WHERE id='$id'");
    //$srow=mysqli_num_rows($query);
    $row=mysqli_num_rows($query);
    
    if($row > 0)
    {
      while($result = mysqli_fetch_array($query)){
      {
        $customer = $result["customer"]; 
        $code = $result["code"];
        $supplier = $result["supplier"];
        $product = $result["product"];
        $qty = $result["qty"];
        $price = $result["price"];
?>
        <tr>
          <td><?php echo $customer ?> </td>
          <td><?php echo $code ?> </td>
          <td><?php echo $supplier ?> </td>
          <td><?php echo $product ?> </td>
          <td><?php echo $qty ?> </td>
          <td><?php echo $price ?> </td>                
        </tr>
<?php
      }
    }
  }
  endforeach;
}
else
{
    echo'
    <tr>
        <td colspan = "4"><center>Record Not Found</center></td>
    </tr>';
}
?>

Now how can I insert all the fetch data into new table in SQL?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    What _specific_ issue are you having right now? What goes wrong in your code exactly? You did not indicate what problem you want to ask about, there is no clear error or unexpected behaviour indicated. We don't know what is going wrong, or what debugging you've done already (I hope you've done some - if not, why not???) or where you need help exactly. You need to give us a much clearer description. Remember we cannot see your data or run your code. See also the [tour], [ask] and how to make a [mre] of your issue, for more guidance. Thanks. – ADyson Dec 21 '22 at 14:16
  • 2
    P.S. **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Dec 21 '22 at 14:16
  • 1
    https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use it again. – ADyson Dec 21 '22 at 14:16
  • 1
    Also, your second screenshot...is that the result you get right now from your code? If so, what do you think is wrong with it? What were you expecting instead? Again, it's not clear what the problem is supposed to be. – ADyson Dec 21 '22 at 14:17
  • 1
    P.P.S. Instead of a separate query to select each ID's data individually, you could make the SQL a lot more efficient by building a single SELECT statement with an `IN` clause, instead. – ADyson Dec 21 '22 at 14:19

0 Answers0