0

I'm attempting to use a form post to add rows to a mySQL table. Each row has a primary I'm calling quoteID. When a new form is submitted, it should add itself as a row in the table, with a quoteID of one greater than the previous quoteID. It current looks something like this:

<?
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);
?>

At the moment there are 4 rows in this table, with quoteID's of 1,2,3 and 4. The odd thing is, if I attempt:

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

The output result is:

Array

1

Why isn't $newOrderID displaying 5? and the value $previousOrderID 4?

Thanks.

1 Answer 1

3

Your uniqueid is called quoteID; you're looking for ID:

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

Try this:

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

You're currently getting 1 because when it's not finding a value, it's returning null, which evaluates to 0 when you add 1 to it.

You can also get around this by making the quoteID field an auto_increment one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.