Jump to content
xisto Community
Sign in to follow this  
djax

Debug The Php Code

Recommended Posts

if($s == "poed") ?{ ?if($c == "relva") ?	{ ?	if($osta <= 4 && $osta > 0) ? ?{ ? ?$sql = "SELECT * FROM charinf WHERE nimi = $flgin"; ? ?$query = mysql_query($sql); ? ?while($q = mysql_fetch_array($query)) ? ?	{ ? ?	$hraha = $q["raha"]; ? ?	} ? ?if($hraha >= $relvahinnad[$osta]) ? ?	{ ? ?	$sql = "INSERT INTO kott (omanik, aid, arv, kat) VALUES ($flgin , $osta , 1 , 1)"; ? ?	mysql_query($sql); ? ?	$jarraha = $hraha - $relvahinnad[$osta]; ? ?	$sql = "UPDATE charinf SET raha = $jarraha WHERE nimi = $flgin"; ? ?	mysql_query($sql); ? ?	}	 ? ?} ?	} ?}

Share this post


Link to post
Share on other sites

Without knowing the error that you were encountering I am going to give you my best explanation of what is wrong.

I highlighted the lines with the problems on it below.


  $sql = "SELECT * FROM charinf WHERE nimi = $flgin";  $sql = "INSERT INTO kott (omanik, aid, arv, kat) VALUES ($flgin , $osta , 1 , 1)";  $sql = "UPDATE charinf SET raha = $jarraha WHERE nimi = $flgin";

Explanation:
When you are working with "" in PHP/MySQL you need to remember that it will treat it as a literal string.
What the code lines above are telling the MySQl to do is to compare two columns together - not the values of a variable in one of the columns ( i.e SELECT all from the charinf table where the table column nimi is equal to the table column with the name of the variable of $flgin). This is not what you are looking to do with this code. If you want to use variables in a MySQL statement within PHP you need to put it in single quotes (' ').




Here is my proposed solution::

if($s == "poed")  {  if($c == "relva")  	{  	if($osta <= 4 && $osta > 0)    { $sql = "SELECT * FROM charinf WHERE nimi = '$flgin' ";    $query = mysql_query($sql);    while($q = mysql_fetch_array($query))    	{    	$hraha = $q["raha"];    	}    if($hraha >= $relvahinnad[$osta])    	{    	$sql = "INSERT INTO kott (omanik, aid, arv, kat) VALUES ('$flgin' , '$osta' , 1 , 1)";    	mysql_query($sql);    	$jarraha = $hraha - $relvahinnad[$osta];    	$sql = "UPDATE charinf SET raha = '$jarraha' WHERE nimi = '$flgin' ";    	mysql_query($sql);    	}	    }  	}  }


NOW - assuming that all your other syntax and stuff is right according to how u wrote the rest of tha page - the code above should work. If not then you would still have had that problem with the code anyways.

Notice from snlildude87:
Posts merged

Share this post


Link to post
Share on other sites

O_o unless they changed it recently (last 2 months or so), then variables are treated as their values only in double quotes, not singles. Ex:

$x = 9;echo "$x";echo "<br>";echo '$x';
should output
9$x

Share this post


Link to post
Share on other sites

That's what dogomchawi is saying. The entire query must be enclosed in double quotes, but field values must be enclosed in single quotes within the double quotes (or it can work the other way, provided you specify either constant values or add them outside the quotes). MySQL requires that string-like values be enclosed in quotes to determine delimitation - numeric values are, obviously, only going to contain numbers (and possible a decimal place), whereas strings can contain spaces and any other number of 'special' characters which MySQL may mistake as indicating it should move to the next field assignment.I hope that makes sense. Oh, and you should -always- use single quotes where possible. Strings contained within single quotes are not checked for variable references etc, so they are processed much more quickly (even if a string contained within double quotes doesn't contain any variable names at all, it is still checked for them as well as other escaped characters, which takes processing power and time).

Share this post


Link to post
Share on other sites

hi, all, I have go through all the code above, yes, that should be careful. If not, would produces a long time debug action. furthermore, I checked that the code have only use only single field of the table, but that select all fields of that, this should make the mysql server generate more data and activity, as many programmer experts said, avoid any unneeded data. That will make server service more client and lower the tco.- hope this help- Eric

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.