2012-09-19 35 views
0

我正在嘗試使用表單發佈將行添加到mySQL表。每一行都有一個主要的我打電話給quoteID。提交新表單時,應將其自身添加爲表格中的一行,其quoteID比前面的quoteID大1。它目前看起來是這樣的:mysql_fetch_assoc從主鍵返回數組

<? 
session_start(); 
if(!session_is_registered(myusername)){ 
header("location:login.php"); 
} 
include 'verify.php'; 
$con = mysql_connect("localhost","user","$password"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("internal", $con); 

$previousOrderID = mysql_query("SELECT * FROM sourcingQuote ORDER BY quoteID DESC LIMIT 1"); 
$previousOrderID = mysql_fetch_assoc($previousOrderID); 
$newOrderID = $previousOrderID['ID'] + 1; 

mysql_close($con); 
?> 

目前有4分排在該表中,用1,2,3 quoteID的和4奇怪的是,如果我嘗試:

<? echo $previousOrderID; ?><br> 
<? echo $newOrderID; ?><br> 

輸出結果是:

Array 

1 

爲什麼不是$ newOrderID顯示5?並且值$ previousOrderID 4?

謝謝。

回答

3

Your uniqueid被稱爲quoteID;你要找的ID:

$newOrderID = $previousOrderID['ID'] + 1; 

試試這個:

$newOrderID = $previousOrderID['quoteID'] + 1; 

您目前得到1,因爲當它沒有找到一個值,它的返回null,它的計算結果爲0時加1到它。

您還可以通過將quoteID字段設置爲auto_increment來解決此問題。

+0

完美:)謝謝! – Hikalea

+0

很高興幫助! – andrewsi