duezy Posted March 4, 2010 Share Posted March 4, 2010 I know most of you guys hate to hear this but I'm relatively new to php, i'm working on a personal site and have created this code that basically removes all members of a softball team. The code works like a charm for me, as it updates all members of a given team id and sets there team and team_id to NULL. This is my question, am I doing this the PHP correct way? Is there another way I could do this that is PHP correct? I could sit on here for light years and ask you experts questions that I have about PHP but i'll spare you guys tonight. Thanks in advance and any help any guidance is appreciated.. $query = "select username, team_id, team FROM tgl_users WHERE team_id = '".$_GET['team_id'] ."'"; $result = mysql_query($query, $dbc); //run the while loop while($r=mysql_fetch_array($result)) { //delete info from all users of team_id $query_update = "UPDATE tgl_users SET team=NULL, team_id=NULL, coach=NULL WHERE team_id = '".$_GET['team_id'] ."'" or die(mysql_error()); mysql_query($query_update, $dbc) or die(mysql_error()); } echo 'team has been deleted and all users have been removed'; } Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/ Share on other sites More sharing options...
trq Posted March 4, 2010 Share Posted March 4, 2010 There is no need to execute any select query or execute your update within any loop. Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021215 Share on other sites More sharing options...
sasa Posted March 4, 2010 Share Posted March 4, 2010 just use $query = "DELETE FROM tgl_users WHERE team_id = '".$_GET['team_id'] ."'"; Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021216 Share on other sites More sharing options...
duezy Posted March 4, 2010 Author Share Posted March 4, 2010 and it will update every member who has the same team_id? Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021221 Share on other sites More sharing options...
trq Posted March 4, 2010 Share Posted March 4, 2010 Indeed. Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021223 Share on other sites More sharing options...
trq Posted March 4, 2010 Share Posted March 4, 2010 ps: You really need to be escaping any outside data using mysql-real_escape_string too. This is simply a security precaution. Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021225 Share on other sites More sharing options...
duezy Posted March 4, 2010 Author Share Posted March 4, 2010 Thanks guys, thorpe.. If you don't mind me asking if you know, how does it know to update every member? Curious Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021226 Share on other sites More sharing options...
trq Posted March 4, 2010 Share Posted March 4, 2010 Thanks guys, thorpe.. If you don't mind me asking if you know, how does it know to update every member? Curious Because you telling it to update everything matching your WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021241 Share on other sites More sharing options...
Rustywolf Posted March 4, 2010 Share Posted March 4, 2010 One thing i know is that $query = "select username, team_id, team FROM tgl_users WHERE team_id = '".$_GET['team_id'] ."'"; $result = mysql_query($query, $dbc); can be shortened to $result = mysql_query("select username, team_id, team FROM tgl_users WHERE team_id = '".$_GET['team_id'] ."'", $dbc); and same with mysql_query("UPDATE tgl_users SET team=NULL, team_id=NULL, coach=NULL WHERE team_id = '".$_GET['team_id'] ."'", $dbc) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/194080-better-way-to-reword-code/#findComment-1021263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.