If/ElseIf statement inside INSERT INTO query?!?

How can I get this to work?

mysql_query("INSERT INTO ".$prefix."_matchplay_userstats (userid,clubid,matchid,result,matchform) 
			  VALUES ('$p1','$clubid','$matchid',";
			  if($winner == 1){
				  mysql_query."'1'";
			  } elseif($winner == 2){
				  mysql_query."'0'";
			  } elseif($winner == 3){
				  mysql_query."'2'";
			  }
			  mysql_query.",'$matchtype')")

;

I get an error regarding this line:

VALUES ('$p1','$clubid','$matchid',";

The error message is: Parse error: syntax error, unexpected ‘;’

Please help :slight_smile:

Yep, your code is all wrong :wink:
mysql_query() is a function call, not a variable. So doing mysql_query.“‘1’”; will not append ‘1’ to your query, it’ll just cause an error. (Even if it were a variable, you concatinate strings with the .= operator)

You need to assemble a query string that you then pass into the [fphp]mysql_query[/fphp] function.
I assume the userid, clubid, matchid and result fields in your DB are all integer types, so you shouldn’t use single quotes around the values in the query.
(You should of course ensure the values are cast as integers too!)


if($winner == 1)         $result = 1;
else if($winner == 2)    $result = 0;
else if($winner == 3)    $result = 2;

$query = "INSERT INTO {$prefix}_matchplay_userstats (userid, clubid, matchid, result, matchform)
VALUES ($p1, $clubid, $matchid, $result, '$matchtype')";

if(mysql_query($query)) {
    //success
}
else {
    //examine mysql_error() to get error message from MySQL
}

Oh yes… Thanks… That works :slight_smile: