简体   繁体   English

将数据从一个表复制到另一个表?

[英]copy data from one table into another?

when a user signs up to my site they complete a registration form and have their details put into a table called ptb_registrations. 用户注册到我的网站时,他们会填写一份注册表,并将其详细信息放入名为ptb_registrations的表中。

after this they receive an email with a verification code, they click the email link and go to the verification link, upon entering the correct code i want a query to run. 此后,他们会收到一封包含验证码的电子邮件,然后单击电子邮件链接并转到验证链接,输入正确的代码后,我要运行查询。

this query needs to act as an insert query that will copy the data from one table (ptb_registrations) into another (ptb_users). 此查询需要充当插入查询,它将数据从一个表(ptb_registrations)复制到另一个表(ptb_users)。

can someone help me with this as it's not working and im not sure if im heading in the right direction. 有人可以帮我解决这个问题,因为它不起作用,我不确定我是否朝着正确的方向前进。 thanks 谢谢

// Make a safe query
$query = sprintf("INSERT INTO ptb_users (first_name, last_name, email,    
    password, dob)
SELECT firstname, lastname, email, password    
    dob
WHERE not exists (select 1 from ptb_registrations where ptb_registrations.registration_code='$verificationcode';",
                    mysql_real_escape_string($newpassword));

mysql_query($query)or die('Could not update members: ' . mysql_error());

As far as I can see, you have a lot of syntax mistakes in your query: 据我所知,查询中存在很多语法错误:

no comma between password and dob in select statement, From table for select statement is not specified. select语句中的密码和dob之间没有逗号,select语句的From表未指定。 Also, where condition looks strange (and there is no closing bracket for not exists statement). 此外,条件看起来很奇怪(并且not exists语句的右括号)。 As for me, you should have a query like: 对于我来说,您应该有一个类似的查询:

$query = "INSERT INTO ptb_users (first_name, last_name, email, password, dob)
SELECT firstname, lastname, email,'". mysql_real_escape_string($newpassword)."', dob FROM ptb_registrations
WHERE ptb_registrations.registration_code=''". mysql_real_escape_string($$verificationcode).";"

This query will take a row from ptb_registrations where registration code is equal to one received from request and insert it into ptb_users (and put a new password as a user password) 此查询将从ptb_registrations中获取一行,其中注册码等于从请求中收到的注册码,然后将其插入ptb_users中(并输入新密码作为用户密码)

You can try something like this: 您可以尝试如下操作:

SELECT firstname, lastname, email, password,dob
into yourtablename
from (tablename)
^ you are missing something here i.e your tablename
WHERE not exists (select 1 from ptb_registrations where
ptb_registrations.registration_code='$verificationcode';",
mysql_real_escape_string($newpassword)
  • This will create new table with same data structure 这将创建具有相同data structure新表

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM