Jump to content

Recommended Posts

I'm setting a _$SESSION variable using the SELECT query below:

 

<?php

//Get last Invoice number from database for informational purposes only

$query_rstLastINVNum = "SELECT ID FROM cue ORDER BY ID DESC LIMIT 1";

mysql_select_db($database_dbconn, $dbconn);

$rstLastINVNum = mysql_query($query_rstLastINVNum, $dbconn) or die(mysql_error());

$row_LastINVNum = mysql_fetch_assoc($rstLastINVNum);

$_SESSION['last_invoice'] = $row_LastINVNum;

?>

 

After submitting the Invoice to the DB I use the variable $_SESSION['last_invoice'] on the Invoice page and the Invoice number is correct ("0000053"), so I think this tells me that the $_SESSION['last_invoice'] variable exists and is populated correctly.

 

However, I'm also using $_SESSION['last_invoice'] to populate a field in the DB before INSERTing the record, in this case the 'last_invoice' variable is wrong ("0000001"), how can that be?

 

The portion of the SQL used for populating the field looks like this:

 

GetSQLValueString(sprintf("%07d",$_SESSION['last_invoice']), "text"),

 

So, to clarify when I enter an invoice into the system and set the session variable using the following $_SESSION['last_invoice'] the database field is incorrect "0000001" and the Invoice on the Invoice page is correct "0000053"... its like there's some type of rounding going on.

 

As a side note the DB field is VARCHAR() and I did try changing how the field was populated and this worked correctly.is populated

GetSQLValueString(sprintf("%07d",53), "text"),

 

This is the function GetSQLValueString()

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

Any help with this one would be appreciated as always.

 

A JM,

 

 

 

 

No reply's -  not a good sign...  :(

 

Can someone explain what the "GetSQLValueString()" is used for?

This is typical Dreamweaver gunk. However it basically used to make data that goes into a query safe for insertion.

This line of code is not doing anything because $row_LastINVNum is an array and you would need to access an element of that array -

 

$_SESSION['last_invoice'] = $row_LastINVNum;

 

Your resultant number is probably 1 because you are likely adding one to that value. Which is in itself another problem because getting the current highest ID and adding one to it and then actually using that value won't be valid when you have concurrent visitors to your site and more than one is causing data to be inserted.

This line of code is not doing anything because $row_LastINVNum is an array and you would need to access an element of that array

 

So how do I access a particular field of the array?

 

I didn't realize I was adding 1 to the Invoice number - I'm a little confused by that can you explain a little more, thanks.

 

Your resultant number is probably 1 because you are likely adding one to that value. Which is in itself another problem because getting the current highest ID and adding one to it and then actually using that value won't be valid when you have concurrent visitors to your site and more than one is causing data to be inserted.

 

That's exactly the point I was trying to make last night on the site... if I grab the ID before the record is submitted and then another record is submitted before it then I'm hosed, how do I resolve that problem?

 

http://us.php.net/manual/en/language.types.array.php Also, the documentation for the function you are using to fetch the data contains an example of accessing the elements of the array that is returned - http://us3.php.net/mysql_fetch_assoc

 

If you are attempting to get the highest ID and add one to it and actually use that value for some purpose, don't. You can only know for certain what an id is after it has been created due to an INSERT query.

I could use a little clarification then.

 

//Get last Invoice number from database for informational purposes only

$query_rstLastINVNum = "SELECT ID FROM cue ORDER BY ID DESC LIMIT 1";

mysql_select_db($database_dbconn, $dbconn);

$rstLastINVNum = mysql_query($query_rstLastINVNum, $dbconn) or die(mysql_error());

 

The above returns a single item single record the last ID since it is auto incremented, no array correct?

 

In this case can I use the variable $rstLastINVNum as the last ID in the table?

 

 

 

//Get last Invoice number from database for informational purposes only

$query_rstLastINVNum = "SELECT ID FROM cue ORDER BY ID DESC";

mysql_select_db($database_dbconn, $dbconn);

$rstLastINVNum = mysql_query($query_rstLastINVNum, $dbconn) or die(mysql_error());

$row_LastINVNum = mysql_fetch_assoc($rstLastINVNum);

 

By removing  LIMIT 1 inthe above this will return all the records in DESC order and mysql_fetch_assoc() adds it to an array, correct? Is it zero based?

 

So would something like this work?

 

while( $row_LastINVNum = mysql_fetch_assoc($rstLastINVNum) {
    // Display.
    $_SESSION['last_invoice'] = $row_LastINVNum[ 0 ];
    break;
}

Replying to my own topic - hoping it might lead to a resolution of my problem... or if someone has some other ideas...

 

I was thinking that I could get the ID number immediately after the INSERT query then I could run an UPDATE query to update the record in my DB...

 

INSERT....

$last_invoice = mysql_insert_id(); 

UPDATE...

 

Any thoughts????

 

A JM,

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.