A JM Posted May 14, 2009 Share Posted May 14, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/ Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 No reply's - not a good sign... Can someone explain what the "GetSQLValueString()" is used for? A JM, Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834258 Share on other sites More sharing options...
wildteen88 Posted May 14, 2009 Share Posted May 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834263 Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 What is get_magic_quotes_gpc() used for? Since the value that was being inserted was a "0000001" or basically "1" I'm guessing it might be generating something along the lines of a TRUE/FALSE value?? All of which is a quess... Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834281 Share on other sites More sharing options...
A JM Posted May 15, 2009 Author Share Posted May 15, 2009 Still trying to iron this out so hopefully someone can supply a little more info. or other ideas as to what's up with the Session variable. Thanks. AJM, Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834454 Share on other sites More sharing options...
PFMaBiSmAd Posted May 15, 2009 Share Posted May 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834458 Share on other sites More sharing options...
A JM Posted May 15, 2009 Author Share Posted May 15, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834464 Share on other sites More sharing options...
PFMaBiSmAd Posted May 15, 2009 Share Posted May 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834474 Share on other sites More sharing options...
A JM Posted May 15, 2009 Author Share Posted May 15, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834482 Share on other sites More sharing options...
A JM Posted May 15, 2009 Author Share Posted May 15, 2009 PFMaBiSmAd, where did you go? Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834632 Share on other sites More sharing options...
A JM Posted May 15, 2009 Author Share Posted May 15, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/158101-_session-variable-question-not-correct-when-used/#findComment-834795 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.