2012-09-19 107 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