使用AI ID字段复制MySQL记录

时间:2012-01-25 15:55:20

标签: php mysql

我正在尝试使用以下函数复制我的MySQL记录。它说我的SQL中有一个错误,我看不到。我在底部添加了一些回声来帮助诊断。此外,是否有更简单的方法来单独命名每一行? (行[$ X])?

该功能基本上是读取发布到它的ID,然后将其写回数据库,就好像用户只是自己输入它而不按下重复按钮。

我已经在堆栈上尝试了其他代码,我想我会写一个我真正理解的代码,这就是它!

编辑:我的第二个查询似乎也是有问题的。

谢谢!

function procDupRec(){

    $id = mysql_real_escape_string($_GET['duplicate']);

    $query = "SELECT * FROM contacts WHERE id=$id";

            $result = mysql_query($query);      

            while($row = mysql_fetch_row($result)){

            $category = $row[1];
            $dateEntered = $row[2];
            $account = $row[3];
            $firstName = $row[4];
            $lastName = $row[5];
            $company = $row[6];
            $titlePos = $row[7];
            $officeNum = $row[8];
            $phoneExt = $row[9];        
            $homeNum = $row[10];
            $mobileNum = $row[11];
            $pagerNum = $row[12];
            $faxNum = $row[13];
            $address1 = $row[14];
            $address2 = $row[15];
            $city = $row[16];
            $state = $row[17];
            $zip = $row[18];
            $email = $row[19];
            $website = $row[20];
            $notes = $row[21];  
            $editBit = $row[22];    
            $protection = $row[23];
            $lastIP = $_SERVER['REMOTE_ADDR'];

            }

            $query2 = mysql_query("INSERT INTO contacts (category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP) VALUES ('$category', '$dateEntered', '$account', '$firstName', '$lastName', '$company', '$titlePos', '$officeNum', '$phoneExt', '$homeNum', '$mobileNum', '$pagerNum', '$faxNum', '$address1', '$address2', '$city', '$state', '$zip', '$email', '$website', '$notes', '$editBit', '$protection', '$lastIP')");

            $result2 = mysql_query($query2);

            echo $query2;

            echo mysql_error();



}

1 个答案:

答案 0 :(得分:1)

执行查询后,回显查询将不起作用。这只会包含一个结果。

您应该在执行之前存储查询,例如

$query2 ="INSERT INTO contacts (category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP) VALUES ('$category', '$dateEntered', '$account', '$firstName', '$lastName', '$company', '$titlePos', '$officeNum', '$phoneExt', '$homeNum', '$mobileNum', '$pagerNum', '$faxNum', '$address1', '$address2', '$city', '$state', '$zip', '$email', '$website', '$notes', '$editBit', '$protection', '$lastIP')";
mysql_query($query2);

使用此功能,您可以使用echo $query2

检查查询

复制的另一种选择:

$query = "INSERT INTO contacts 
                    (category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP) 

                    (SELECT category, dateEntered, account, firstName, lastName, company, titlePos, officeNum, phoneExt, homeNum, mobileNum, pagerNum, faxNum, address1, address2, city, state, zip, email, website, notes, editBit, protection, lastIP FROM contacts WHERE id=$id)" 
mysql_query($query);