[英]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.