2011-06-08 57 views
1

(當我寫這篇文章的時候,它對MSSQL數據庫沒有任何問題,現在我正在使用SELECT LAST_INSERT_ID()爲MySQL寫同樣的東西。而不是@SCOPE_IDENTITY) 我有一段代碼進入公司,然後是該公司的客戶詳細信息。第一個SELECT LAST_INSERT_ID()適用於將company_id從公司表插入到客戶表中,但隨後會引發錯誤以檢索client_id。SELECT LAST_INSERT_ID()在直接MySQL查詢中工作,但不是間歇性地工作PHP QUERY

下面的代碼: ////設置初始SQLS現在

$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','','[email protected]','[email protected]','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數據庫時工作正常。

任何幫助將不勝感激。

感謝

安迪

回答

1

您不能運行在同一時間2個查詢與mysql_query() 但是你可以用PDO做到這一點。

爲了達到你的目的,你可以完全放下SELECT LAST_INSERT_ID() as NEWID和使用mysql_insert_id()

+0

感謝第二。好吧,如果我能夠用MSSQL_QUERY做到這一點,那麼MySQL語句怎麼不讚賞呢? – 2011-06-08 13:59:43

2

您要一氣呵成發送多個查詢。從文檔:

的mysql_query()發送一個唯一的查詢(多個查詢不支持)

,你可能還需要考慮使用mysql_insert_id函數,而不是發出手動查詢:http://www.php.net/manual/en/function.mysql-insert-id.php

+0

您好,唉唉感謝。我已經研究過這個,但是想看看我能否先弄清楚這一點。 MSSQL_QUERY語句可以處理多個查詢,所以想象MYSQL_QUERY也可以。 – 2011-06-08 13:58:18

0

SELECT LAST_INSERT_ID ()是一個在mysql中運行的查詢,但php只允許您一次運行一個查詢,因此在查詢中運行SELECT LAST_INSERT_ID()不起作用。

我的建議是將一個變量賦值給SELECT LAST_INSERT_ID()的值並在查詢中插入該變量。

+0

感謝您的回覆。 – 2011-06-08 14:04:03

0

這不是SELECT語句的錯誤,而是您一次執行兩條語句的事實,這是PHP的MySQL庫所不允許的。只要做兩個不同的查詢來解決這個問題。

+0

謝謝。 MSSQL_QUERY允許我這樣做,這就是爲什麼我感到困惑。 – 2011-06-08 14:03:25

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); 
+0

嗨,謝謝。我確實關閉了插入標記,但是如上所述,響應表示我不允許在同一個函數中使用兩個MySQL SQL語句......我想我可以做你的解決方案或者像上面那樣使用PHP版本mysql_insert_id。(翻轉硬幣) – 2011-06-08 14:02:36

+0

您是否在單個查詢中輸入了編號? – K6t 2011-06-08 14:04:43

+0

是的MSSQL_Query可以做到這一點。 – 2011-06-08 14:08:59

相關問題