基于两个表的PHP / MySQL更新和插入

时间:2012-01-07 00:32:24

标签: php mysql

我已经过了几天了,并且一直在磕磕绊绊。我试图从table2中选择一个唯一的id,并将它与table1中的id匹配。如果id匹配,则更新table1中的行并从table2中删除记录。如果没有匹配,则将记录插入table1。

我已经到了可以更新记录并插入新记录的点,但我似乎无法在插入之前删除匹配的记录,因此创建了一个副本。我在更新后尝试了删除连接查询,但由于新的logon_id前面有一个字符,因此它不再与table2匹配。

我之前正在进行更新加入更新,但是我发现了太多查询,所以试图保持简单。

有什么建议吗?在这场比赛中还是一个新手。

$table2_query   =   "SELECT * FROM table2";
$table2_result  =   mysql_query($table2_query);
$table2_count   =   mysql_num_rows($table2_result);

    if($table2_count == 0)      {

        if(mysql_error())   {
        echo 'Error: '.mysql_error();   
        }

    }

while($table2_row = mysql_fetch_array($table2_result, MYSQL_ASSOC))     {

    $check_number_length = strlen($table2_row['unique_id']);

        if($check_number_length < 7)    {

            if(substr($table2_row['unique_id'], 0, 2) < 35) {
                $logon_id   = 'n' . $table2_row['unique_id'];
            }else   {
                $logon_id   = 'v' . $table2_row['unique_id'];
            }

        }else   {
            $logon_id   = $table2_row['unique_id'];
        }

    // Set variables for insert query for creation of a new user record
    $first_name     = $table2_row['firstname'];
    $last_name          = $table2_row['lastname'];
    $email_address  = $table2_row['email'];


    $duplicates_query   = "SELECT * FROM table1 WHERE '$logon_id' = logon_id";
    $duplicates_result  = mysql_query($duplicates_query);
    $duplicates_row = mysql_fetch_array($duplicates_result);
    $duplicates_count   = mysql_num_rows($duplicates_result);

        if($duplicates_count == 0)  {

            if(mysql_error())   {
            echo 'Error: '.mysql_error();   
            }

        }else   {
            $update_records_query       =   "UPDATE table1 SET first_name='$first_name', last_name='$last_name', email_address='$email_address'";
            $update_records_result      =   mysql_query($update_records_query);
            $update_records_count       =   mysql_affected_rows();
            echo $update_records_count;
        }

    $create_records_query = "INSERT INTO table1 (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";
    $create_records_result  =   mysql_query($create_records_query);
    $create_records_count   =   mysql_affected_rows();

        if($create_records_count == 0){

            if(mysql_error()){
                echo 'Error: '.mysql_error();   
            }   
        }

    echo $create_records_count . ' record(s) created.';

}

1 个答案:

答案 0 :(得分:0)

 $res = mysql_query ("SELECT count(table1.login_id) AS count FROM table2 LEFT JOIN table1 ON table2.login_id = table1.login_id WHERE table2.login_id = \"".$login_id."\";") or die ("Error joining tables"); 
    /*joins both tables together and selects both id's from tables */

    $row = mysql_fetch_assoc($res); // should only return one row so grab first

    if ($row['count'] > 0) { // check if id exists in both tables (duplicates)
             // updates rows from one table into another
             mysql_query('UPDATE table1,table2 SET table1.username = table2.username, table1.password = table2.password, table1.email = table2.email WHERE table1.login_id = "'.$login_id.'" AND table2.login_id = "'.$login_id.'";') or die ("error updating table1");

            //delete old row
             mysql_query('DELETE FROM table2 WHERE login_id = "'.$login_id.'";') or die("error deleting from table2");


    }else { // if id doesn't exist in table1
            mysql_query ("INSERT INTO table1(username,password,email) SELECT username,password,email FROM table2 where login_id = '".$login_id."';") or die ("error inserting into table1");
    }

删除table1上login_id开头的n

UPDATE table1 set login_id = replace(login_id,"n","");

如果在login_id

的开头删除n,则为新的更新查询
mysql_query ('UPDATE table1,table2 SET table1.username = table2.username, table1.password = table2.password, table1.email = table2.email WHERE table1.login_id = table2.login_id AND table1.login_id = "'.$login_id.'";');