简体   繁体   中英

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.

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).

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. Also, where condition looks strange (and there is no closing bracket for not exists statement). 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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