See if data exists in table

Ok, here’s my 2 problems. I am making an online game, and in the game players can start up their own business. What the form does is make sure the user has entered at least $75,000 capital, then checks to make sure that the business name doesn’t already exist in the table “companies”, then if all that is correct it inserts their data into the table and takes away the X amount of money that they spent on capital. It also takes away 50 of their strength. Now These are my questions:

1.) I have the code to check if the business name already exists, and it works, but it’s not displaying the error I told it to.

$result = mysql_query($query); 
if(!$result) 
{ 
   if(mysql_errno() == 1062)  // duplicate entry error 
   { 
     echo "Company name already exists!"; 
   } 
}  

2.) Now my main question. I want to do a check to see if the player has at least $75000 and 50 strength. If they do, it continues with entering the data into the table and taking away their money. If they don’t, an error displays.

Are you 100% sure that the error is 1062?

Instead of relying on an error number (which could change with updates and server versions) you would be better off coding for the “SUCCESS” of events.

By that I mean check to see if the data already exists in the database instead of relying on inserting it and checking the error.

i.e. (pseudo code)

SELECT BusinssName FROM table WHERE BusinessName = "XYZ Business"
if return record count is > 0 then echo error message
else Insert record

You use the ERROR code (generically) as a last step in reporting problems

As for your number 2 question, more detail is needed. Are they entering this data in a form that you want to validate prior to submission or is data that already exists somewhere (like a DB) that you need to check against?

I just got that first code off the internet. Have no idea about this kind of stuff. As for the 2’nd one, the user already has that info in a DB. If they don’t have at least 75,000 money and 50 strength in the database at the time of submission of the form, an error displays that states they don’t have the required strength and money.

What did the author of the script answer? I presume you’ve asked them first about your issues with their script before coming to us for help.

No, he isn’t reachable. So I have no idea :(

How about something along the lines of:

$sql = "SELECT * FROM table WHERE userid='UserX'";
$result = mysql_db_query($database, $sql);
$data = mysql_fetch_assoc($result);
If ($data['money'] < 75000 || $data['strength'] < 50 ){
   echo "You don't qualify to continue";
   // Do stuff here to exit
} else {
   echo "Congratulations.... You qualify to continue";
   // Do stuff here to continue
}

(Code has NOT been tested but is just an example of a direction to try.)

Thanks, I’ll edit that to my needs a little and try it. Thanks, I’ll get back to you and tell you what happened.

Sponsor our Newsletter | Privacy Policy | Terms of Service