2个mysql查询中的1个变量,有帮助吗?

$qry1 = "DELETE FROM
    si_topics
WHERE
    topic_id = '". $_GET['topic'] ."'
DELETE FROM 
    si_posts
WHERE
    topic_id = '". $_GET['topic'] ."'";

$mysqlqry1 = mysql_query($qry1);
if($mysqlqry1){
    echo 'Topic deleted from database!';
} else {
    echo 'Mysql query failed!';
}

This gives the message Mysql query failed!, why?

Greetings

You are missing a semicolon between your two delete statements.

Also, multiple queries are not supported in mysql_query.

From the documentation:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.`

You can not run multiple queries in mysql_query() function in once.

Try mysqli::multi_query instead to run multiple queries. queries can be separated by ;

Add mysql_error ($link) after 'echo 'Mysql query failed!';' line

That will explain the problem.

PHP does not allow running 2 queries in one go.

You need to split your queries in 2 variables and run them one by one.

http://php.net/manual/en/function.mysql-query.php

The documentation explains:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

The standard MySQL separator semicolon ( ; ) will not work here.

Quoting from the manual (my emphasis)

mysql_query() sends a unique query (multiple queries are not supported)

If you want to execute several SQL statements in a single query, you need to use mysqli and multi-query, and each statement needs to be separated by a semi-colon (;)

Because:

  • you're sending one query with invalid syntax, and
  • even if you'd added the semicolon between queries (how else is the MySQL server supposed to know where your second query starts?!?), PHP's old library doesn't support that here.

Use mysqli.multi_query() instead.

You should also either use prepared statements or at the very least sanitise your inputs, as your code has a huge SQL Injection vulnerability (how are people still writing PHP like this?!? what are they teaching in these "schools"?! aghh!)

If you wish to specify multiple delete conditions across several tables, then you can use JOIN syntax for the DELETE statement, however this might be inefficient - but that way, the query will fit within mysql_query();

Someone already mentioned mysqli and multi_query function, thumbs up for that :)

On the other hand, your code is extremely prone to SQL injection attacks, always clean your $_POST and $_GET input before using it in the query directly!