I have two delete function and would like to make this as one. So I can use this on my webpage. But not sure how? Anyone willing to help me :D

DELETE
       FROM test
       WHERE 
       Tes_ID=$Tes_ID;

     DELETE
       FROM question
       WHERE 
       Tes_ID=$Tes_ID

Dani AI

Generated

A concise recap and practical guidance.

’s single-statement solution is the clean fix here: combining the deletes into one SQL operation removes matching rows from both tables in a single pass and avoided the syntax error was seeing. Alternatives worth considering are (a) enforcing referential integrity so child rows are removed automatically via a foreign key with ON DELETE CASCADE, or (b) issuing two deletes from the application inside a transaction so both succeed or both roll back.

Troubleshooting pointers for the original #1064 error and for reliable deletes:

  • Log or echo the exact SQL string PHP sends to MySQL and run it directly in the MySQL client to reproduce the syntax error. That isolates PHP interpolation/quoting issues from SQL syntax problems.
  • Check aliasing and statement form: some DELETE syntaxes differ from SELECT/UPDATE; an alias used incorrectly can trigger a parser error.
  • Verify storage engines: InnoDB supports transactions and foreign keys; MyISAM does not. If you need transactional safety or ON DELETE CASCADE, use InnoDB.
  • If executing multiple statements from PHP, use the proper API (mysqli_multi_query or separate prepared statements) and always check returned errors/affected rows.

Practical cautions: back up data before running mass deletes, test on a staging copy, and prefer parameterized queries (PDO/mysqli) over string interpolation. Note that ’s ROLLUP suggestion is unrelated — ROLLUP is an aggregate/ GROUP BY feature and does not perform deletions.

Recommended Answers

All 7 Replies

Deferent tables deferent queries... Why that bothers you?

I think Tes_ID is common for both tables. If so, try with the below query.

DELETE FROM test as t1
 WHERE t1.Tes_ID = '$Tes_ID' 
   AND t1.Tes_ID IN (SELECT t2.Tes_ID 
                     FROM question as t2
                    WHERE t2.Tes_ID = '$Tes_ID')
Member Avatar for Member #120589

You may want to look at the ROLLUP function in mysql. You need foreign keys I think.

: because I am trying to delete both table using PHP and it doesn excute both of the query.

@paulraj.. I am getting an error message, which I been getting for all the other methods I have tired.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as t1 WHERE t1.Tes_ID = 12 AND t1.Tes_ID IN (SELECT t2.Tes' at line 1

@ardav... I do have foreign key. These two tables are actually linked by Tes_ID.

Member Avatar for Member #120589

I think they need to be constrained though. ROLLUP should automatically delete any related records.

, I edited my post by using joins to delete the records. Here is below,

DELETE t1, t2 FROM 
	`question` AS t1 
	LEFT JOIN `test` AS t2 
	ON t1.Tes_ID = t2.Tes_ID 
	WHERE t1.Tes_ID = '1'

@ardav.. Thank you for the help

@paulraj... it worked. Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.