Web Hosting Talk







View Full Version : Simple php mysql help please


lexington
04-18-2007, 09:48 PM
It seems that every programmer that I pay is away so I am trying my best to figure out some things myself but having problems. Basically I created a simple line of code that deletes data from another database when ran. (replace blah with real name of db)

mysql_query("DELETE from blah_table where blah_name = '".$_post['band_name']."' LIMIT 1");

That works, but there are other db tables that only contain the user_id and not the blah_name table. So I used this code:

mysql_query("SELECT user_id FROM blah_table WHERE blah_name = '".$_post['band_name']."' LIMIT 1");

That works as well, but how can I write the script so that it detects the user_id from the other database and deletes other data from other tables? I can enter the table data myself but I do not know how to write the script to detect the user_id to delete from other tables. That way the other commands would be like:

mysql_query("DELETE FROM other_table WHERE user_id= 'USER ID HERE' LIMIT 1");

zacharooni
04-18-2007, 10:05 PM
You need to do the query, and hold the value in a variable, get the other field names for user_id, then run multiple queries.

lexington
04-18-2007, 10:09 PM
Could you give me an example please? Because I thought I was doing this correctly but it seemed like the user_id field was always blank so I am not sure what I am doing wrong to pull the data.

foobic
04-18-2007, 10:26 PM
You could just use a subquery:
DELETE FROM other_table
WHERE user_id = (
SELECT user_id FROM blah_table
WHERE blah_name = '$band_name'
LIMIT 1
)
LIMIT 1;

Since you apparently have no input validation you'd want to be really careful not to make these scripts available to the public.

tnndotnet
04-20-2007, 05:09 PM
mmmmm subqueries...

Writing php for quite some time now, and I feel like a complete retard that I did not know this was possible...

Thanks for that post!