如何在执行MYSQL查询之前检查条件? [关闭]

I am trying to check to see if a condition exists before executing a mysql update. This code below WORKS:

if (!mysql_query(
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

{
die('Sorry, a database error occurred');
}

...but I'm trying to add another condition to it like this:

if (!mysql_query && ($tradein_status == 'Accepted CREDIT')(
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

{
die('Sorry, a database error occurred');
}

..which DOESN'T work - it generates this error: "Parse error: syntax error, unexpected '(' in /home/bohemeth/public_html/tradeins/save_tradeins.php on line 235"

The second thing I am wondering is why is this part:

if (!mysql_query(
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

...not written with two parens and curly braces after the condition, which it seems you would do anywhere else for a conditional test, like so:

if (!mysql_query){
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
}

THANK you for any help!!

mysql_query is a function. It's used like this: mysql_query('SQL QUERY');

It was written like in your question as an attempt to make it more readable:

if(!mysql_query(
'SQL QUERY'
))

It could also be written this way:

if(!mysql_query('SQL QUERY'))

So, to add another clause, add it before or after the mysql_query call.

if($tradein_status == 'Accepted CREDIT' && mysql_query('SQL QUERY'))

In an if statement the {} can be omitted. In that case, the very next line is the one ran if the case is true.

So, to finalize:

if($tradein_status == 'Accepted CREDIT' && mysql_query('SQL QUERY')){
    die('Sorry, a database error occurred');
}
if ($tradein_status == 'Accepted CREDIT')
    mysql_query("UPDATE customers SET available_credit = available_credit where userid = '$userid';") or die("Sorry, a database error occurred");

You cannot write other code between the function name and the arguments.

mysql_query is a function call, which passes your query "UPDATE customers SET available_credit = available_credit where userid = '$userid';" as a string parameter.

This means that mysql_query must be of the form:

mysql_query("UPDATE customers SET available_credit = available_credit where userid = '$userid';")

mysql_query returns a RESOURCE.. which is the result of your query.

In order to add a second conditional statement, do this:

if (!mysql_query("UPDATE customers SET available_credit = available_credit where userid = '$userid';") && ($tradein_status == 'Accepted CREDIT'))
if ($tradein_status == 'Accepted CREDIT' && !mysql_query (
"UPDATE customers SET available_credit = available_credit where userid = '$userid';"
))

{
die('Sorry, a database error occurred');
}

mysql_query is a function, not a variable. The string in the second parens is the argument.