blue-genie Posted September 28, 2009 Share Posted September 28, 2009 don't laugh but i figured i want to get the autoincrement value of what i just inserted and repeat it in another field. so i do the insert , then mysql_insert_id(); then another update. can someone clean this up for me so it works all the time (currently i get a null sometimes though the insert does happen) <?php session_start(); include 'config.php'; include 'opendb.php'; $gameID= $_REQUEST['gameID']; $gameRNG= $_REQUEST['gameRNG']; $insert = mysql_query("INSERT INTO gameinstance (gameID, gameRNGResult, gameDateTime) VALUES ('$gameID', '$gameRNG', now())"); if(!$insert){ echo '<?xml version="1.0"?>'; echo '<dataxml>'; die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; echo '</dataxml>'; } else { $gameInstanceNo = mysql_insert_id(); mysql_query("UPDATE gameInstance SET gameInstance_No='$gameInstanceNo' WHERE USN_Gameinstance='$gameInstanceNo'"); echo '<?xml version="1.0"?>'; echo '<dataxml>'; $success = mysql_insert_id(); echo "<success>".$success."</success>"; echo '</dataxml>'; } ?> Link to comment https://forums.phpfreaks.com/topic/175794-this-code-works-sporadically-so-im-guessing-its-not-right/ Share on other sites More sharing options...
Dtonlinegames Posted September 28, 2009 Share Posted September 28, 2009 Try <?php session_start(); include 'config.php'; include 'opendb.php'; $gameID= $_REQUEST['gameID']; $gameRNG= $_REQUEST['gameRNG']; $insert = mysql_query("INSERT INTO gameinstance (gameID, gameRNGResult, gameDateTime) VALUES ('$gameID', '$gameRNG', now())"); if(!$insert){ echo <?xml version="1.0"?>'; echo '<dataxml>'; die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; echo '</dataxml>'; } else { $gameInstanceNo = $insert; mysql_query("UPDATE gameInstance SET gameInstance_No='$gameInstanceNo' WHERE USN_Gameinstance='$gameInstanceNo'"); echo '<?xml version="1.0"?>'; echo '<dataxml>'; $success = mysql_insert_id(); echo "<success>".$success."</success>"; echo '</dataxml>'; } ?> Link to comment https://forums.phpfreaks.com/topic/175794-this-code-works-sporadically-so-im-guessing-its-not-right/#findComment-926346 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2009 Share Posted September 28, 2009 Dtonlinegames, the change you made to the code is meaningless because the value returned by an insert query is only a TRUE or FALSE value, depending on if the query succeeded or failed. mysql_insert_id() exists and was being used for a very specific purpose in the code. blue-genie, what operating system are you using because the table names gameinstance and gameInstance are not the same on operating systems that are case-sensitive. Link to comment https://forums.phpfreaks.com/topic/175794-this-code-works-sporadically-so-im-guessing-its-not-right/#findComment-926353 Share on other sites More sharing options...
blue-genie Posted September 28, 2009 Author Share Posted September 28, 2009 i'm using a linux server in my final thing. testing on xampp on XP in localhost. maybe that's the problem will test it this evening and let you know. Link to comment https://forums.phpfreaks.com/topic/175794-this-code-works-sporadically-so-im-guessing-its-not-right/#findComment-926386 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2009 Share Posted September 28, 2009 I'll guess that on the live server the UPDATE query is failing because the table by that name/capitalization does not exist. You are looking in the first/only table for a value as the result of the UPDATE query that will never be present because it was attempted on a different table. Link to comment https://forums.phpfreaks.com/topic/175794-this-code-works-sporadically-so-im-guessing-its-not-right/#findComment-926396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.