SELECT LAST_INSERT_ID()适用于Direct MySQL Query,但不能间歇性地使用PHP QUERY

时间:2011-06-08 13:37:53

标签: php mysql

(在我写这篇文章的时候记住它对MSSQL数据库没问题,现在我用MySQL LAST_INSERT_ID()而不是@SCOPE_IDENTITY为MySQL编写相同的东西) 我有一段代码进入公司,然后是该公司的客户详细信息。第一个SELECT LAST_INSERT_ID()可以很好地将company_id从companies表插入到clients表中,但是然后抛出一个错误来检索client_id。

这是代码:     ////设置初始SQL

$SQLINSERTCOMPANY = "insert into companies(company_name,is_organisation,company_regno,company_url,is_active,entry_date)
            VALUES ('$company_name',$is_organisation,'$company_regno','$company_url',1,'$entry_date');";

$SQLINSERTCOMPANYDETAILS = "insert into clients (client_name,client_tel,client_mobile,client_email,client_usern,client_pwdx,mailing_list,savedsession,salt,company_id)
            VALUES ('$client_name','$client_tel','$client_mobile','$client_email','$client_email','$psw',$sign_up,'$PHPSESSIONID','$salt'";

$SQLUPDATECOMPANY = "update clients 
        set company_name = '$company_name',
        is_organisation = $is_organisation,
        company_regno = '$company_regno',
        company_url = '$company_url',
        is_active = 1,
        entry_date = '$entry_date' ";

$SQLUPDATECOMPANYDETAILS = "update clients 
            set client_name = '$client_name',
            client_tel = '$client_tel',
            client_mobile = '$client_mobile',
            client_email = '$client_email',
            client_usern = '$client_email',
            client_pwdx = '$psw',
            mailing_list = $sign_up,
            savedsession = '$PHPSESSIONID' ";

////get the cart for later use if the cart has items
$cart = $_SESSION['cart'];
if($cart){
    $strSQL = "select *
        from category_items 
        where category_item_id IN ($cart) order by category_items.item_code;";          
    $query_get_value = mysql_query($strSQL) or die ('query failed ' . mysql_error());
}   

///// add a new client here
if($_POST['action'] == "add"){  

    ////// Insert new company or just update the details
    if($no_of_client_entry == 0) {

        ////// update clients
        $strSQL1 = $SQLINSERTCOMPANY;

        ////// Insert Client Details
        $strSQL2 = $SQLINSERTCOMPANYDETAILS . ",(SELECT LAST_INSERT_ID())); SELECT LAST_INSERT_ID() as NEWID;";

        $sent_to = $client_email;
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
        $headers .= "FROM: TheImageLounge<no-reply>";
        $subject = "Welcome to The Image Lounge";
        $message = "<html><head><title>Error Report</title>";
        $message .= "<style type='text/css'>
                        body{margin: 0 auto;text-align:left;}
                        h1, h2, h3 { margin:10px;padding:10px;background-color:#ccc;}                   
                     </style>";
        $message .= "</head><body>";

        $message .= "<p>Welcome to Website... TBC/p>";
        $message .= "<p>Your Details Are:</p>";
        $message .= "<p>Your username: $client_email<br />";
        $message .= "<p>Your password: " . $_SESSION['client_psw'] . "</p>";
        $message .= "<p>We hope that you will enjoy using our website.</p>";
        $message .= "<p>Thank You and best regards,<br />";
        $message .= "<p>The Website.</p>";
        $message .= "</body></html>";

        $sentOK = mail($sent_to,$subject,$message,$headers);    

    } else { ///// just update the client details

        $client_id = $clients_rows['client_id'];

        ////// update clients
        $strSQL1 = $SQLUPDATECOMPANY . " where client_id = $client_id;";

        ////// Insert Client Details
        $strSQL2 = $SQLUPDATECOMPANYDETAILS . " where client_id = $client_id; SELECT client_id as NEWID from clients where client_id = $client_id;";            

    } ///// End if
    ///echo $strSQL2;
    ///// Run transactions

    echo $strSQL1 . "<br />";
    echo $strSQL2;

    $query_insert_client = mysql_query($strSQL1) or die('' . mysql_error());
    $query_insert_clients = mysql_query($strSQL2) or die('' . mysql_error());   

    $last_client = mysql_fetch_assoc($query_insert_clients);
    $client_id =  $last_client['NEWID'];
    mysql_free_result($query_insert_clients);   

现在当我输出SQL时,它看起来像这样:

insert into companies(company_name,is_organisation,company_regno,company_url,is_active,entry_date) VALUES ('Individual',0,'','',1,'2011-06-08 14:16:33');

insert into clients (client_name,client_tel,client_mobile,client_email,client_usern,client_pwdx,mailing_list,savedsession,salt,company_id) VALUES ('Andis','0777','','andrew@com','andrew@com','6be86ab8355979352cdd28bbe7026be71a196936fc1ab0c315629f68c0d5f9162acd20b201a27d0ced0cfd53a8e880a1e8bf3714833ed9c4cbe6d0fe3bf15ca0',0,'8730dfec5ce01b315e1a31007185c486','SALTYDOG',(SELECT LAST_INSERT_ID())); SELECT LAST_INSERT_ID() as NEWID;

但后来我收到了这个错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; SELECT LAST_INSERT_ID() as NEWID' at line 2

如果我将输出的SQL复制并粘贴到MySQL中,它可以正常工作。所以我不确定为什么在第二个查询中失败的原因。使用MSSQL数据库时,这可以正常工作。

任何帮助都会感激不尽。

感谢

ANDI

5 个答案:

答案 0 :(得分:2)

您正在尝试一次发送多个查询。来自文档:

mysql_query()发送一个唯一的查询(不支持多个查询)

您可能还想考虑使用mysql_insert_id函数而不是发出手动查询:http://www.php.net/manual/en/function.mysql-insert-id.php

答案 1 :(得分:1)

error:

 1. u did not close the insert tag

just do simple change
$addorder = "INSERT INTO table_name () 
                        VALUES  ()"; 
$id = "SELECT LAST_INSERT_ID() as new_id"; 
mysql_query($addorder) or die("not added in to the orders db:<br />".mysql_error()."<br />".$addorder."<hr />"); 
echo($id); 

答案 2 :(得分:1)

mysql_query()您无法同时运行2个查询 但是你可以用PDO来做。

要实现目标,您可以完全放弃SELECT LAST_INSERT_ID() as NEWID并使用mysql_insert_id()

答案 3 :(得分:0)

SELECT LAST_INSERT_ID()是一个在mysql中运行的查询,但php只允许您一次运行一个查询,因此在查询中运行SELECT LAST_INSERT_ID()不起作用。

我的建议是将一个变量赋值给SELECT LAST_INSERT_ID()并在查询中插入变量。

答案 4 :(得分:0)

这不是SELECT语句错误,事实上你一次做两个语句,这是PHP的MySQL库所不允许的。只需执行两个不同的查询即可解决问题。