使用php mysql从两个表中获取值插入到单个表中

时间:2014-12-23 12:21:24

标签: php mysql

使用唯一ID从两个表中获取值并插入另一个表中,这是我的代码,例如:" table1"值和" table2"值复制并插入" table3",最后从旧表中删除原始值。

 <?php
ini_set('display_errors', 0);
include 'connection/db_connection.php';
$memberid=$_POST['memberid'];
$query=mysql_query("INSERT INTO existing_member (member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date)
 SELECT member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date FROM member_registration
 WHERE member_id='".$memberid."'" ); 

 /*
$query2=mysql_query("INSERT INTO existing_member (membership_type) SELECT membership_type  FROM membership_details
WHERE member_id='".$memberid."'"); */

 $query2=mysql_query("INSERT INTO existing_member (membership_type)
 SELECT a.membership_type as membership_type FROM membership_details a
INNER JOIN member_registration c ON c.member_id = a.member_id, c.member_name=a.member_name
where a.member_id='".$memberid."'");

if($query2)
{ 
    //$sql=mysql_query("DELETE FROM member_registration WHERE member_id='".$memberid."'");
    //$sql1=mysql_query("delete from membership_details where member_id='".$memberid."'");

    echo "<script>
                alert('Data Deleted Successfully');
                window.location.href='close_member.php';
                </script>"; 
} 
else
{
echo "User Data Already Exist";
}

1 个答案:

答案 0 :(得分:0)

您的第一个查询中有拼写错误:INSERT INTO existing_membe在其末尾忘记了r

此处还缺少yif($quer and $query2)

注意

  • 不要使用mysql函数,不推荐使用它们。请改用mysqliPDO

  • 为避免sql注入攻击,请通过GETPOST转义来自用户的变量,或使用预准备语句。