简体   繁体   English

如何使用php将一个表中的选定数据插入另一个表?

[英]How to insert selected data from a table into another table using php?

I want to insert selected data from one of my table under the same database. 我想在同一数据库下的一个表中插入所选数据。 I have a code but it doesn't work. 我有一个代码,但是没有用。

moduleindex.php moduleindex.php

<?php
    include "connectdatabase.php";//include the connection file
    $db = mysql_select_db("section_masterfile",$connectDatabase);

    $queries = "Select student_Firstname, student_Lastname, student_Section FROM masterfile_table";

    if(isset($_POST['TeacherName'])&& isset($_POST['SectionName']))
    {
     $searchTeacher = $_POST['TeacherName'];
     $searchSection = $_POST['SectionName'];
     $queries = "Select student_Firstname, student_Lastname, student_Section FROM masterfile_table WHERE section_Teacher = '{$searchTeacher}' AND student_Section='{$searchSection}'";
    }

    $queried = mysql_query($queries,$connectDatabase);

?>

insertdata.php insertdata.php

<?php
    include 'moduleindex.php';
    include 'connectdatabase.php';

    $db_pdf = mysql_select_db("section_masterfile",$connectDatabase);


     while($row = mysql_fetch_array($queried))
    {
         $field1 = $row['student_Lastname'];
         $field2 = $row['student_Firstname'];
         $field3 = $row['student_Section'];
         $insert_query = mysql_query("INSERT INTO pdf_table (student_Lastname,student_Firstname,student_Section) VALUES ('{$field1}', '{$field2}', '{$field3}')",$connectDatabase);
     }
?>

You can use single query for both purpose, Try something like this 您可以同时使用单个查询,尝试这样的事情

INSERT INTO pdf_table (student_Lastname, student_Firstname, student_Section )
   SELECT student_Lastname, student_Firstname, student_Section FROM masterfile_table

You can also add the conditions with your select query as per your requirement. 您还可以根据需要在选择查询中添加条件。

There is a mechanism in sql/mysql called select into, search it on Google. 在sql / mysql中,有一种名为select into的机制,可以在Google上进行搜索。

I am not sure what you are trying to do this is the approach. 我不确定您要这样做是什么方法。

  INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

As per an example taken from: 根据以下示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php将来自mysql的选定数据插入mysql中的另一个表? - How to insert selected data from mysql with php to another table in mysql? 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php 如何将数据从一个表插入到另一个表 - How to insert data from a table to another table 我想将表单中的数据插入表中,然后从另一个表中选择另一个数据并使用PHP插入到第三张表中 - I want to insert a data from a form into a table, and select another data from another table and insert into third table using PHP PHP 将数据从一个表插入到另一个表 - PHP Insert data from one table to another PHP / SQL使用准备好的语句从表单插入数据并从另一个表插入数据 - PHP/SQL Insert data from form and insert data from another table using prepared statement MySQL将数据从一个表插入到另一个选定的行 - Mysql insert data from one table to another selected rows PHP:从表单和同一表中的另一个表插入数据 - PHP: Insert data from form and another table in the same table PHP MySQL将数据从一个表插入到另一个表 - PHP MySQL insert data from one table to another table 从db表1中选择数据并将其插入到php中的另一个db表2中 - Select data from db table 1 and insert it into another db table 2 in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM