Jump to content

this code works sporadically so i'm guessing its not right


blue-genie

Recommended Posts

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>';
}

?> 

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>';
}

?> 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.