php? Posted January 1, 2008 Share Posted January 1, 2008 Okay the script below shows a list of usernames that are taken from a whole table in my database. I want to be able to click buttons 'Accept' which would move the username into a different table. 'Decline' should delete the username from the entire database. Any Ideas on how I can accomplish this? <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("login", $con); if ( !$result = mysql_query("SELECT * FROM users") ) { die('MySQL Error: ' . mysql_error()); } while($row = mysql_fetch_array($result)) { echo "<b>" . $row['1']; echo "</b> "; echo "<input type=submit name=Accept value=Accept"; echo " <input type=submit name=Decline value=Decline"; echo " <br />"; } mysql_close($con); ?> Code for deleting from database possibly? if submit value == decline then mysql_query("DELETE FROM users WHERE username='DUNNO'"); Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/ Share on other sites More sharing options...
Northern Flame Posted January 1, 2008 Share Posted January 1, 2008 <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("login", $con); if(!isset($_POST['submit'])){ if ( !$result = mysql_query("SELECT * FROM users") ) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['1']; echo "</b> "; echo "<input type=radio name=\"". $row['1'] ."\" value=1>Accept<br>"; echo " <input type=radio name=\"". $row['1'] ."\" value=2>Decline<br>"; echo " <br />"; } echo "<br><input type=submit name=submit value=Submit>"; } else{ foreach($_POST as $key => $val){ if($val == 1){ echo "Inserting $key into new table...."; mysql_query("INSERT INTO new_table(user) VALUES('$key')"); // Moves user to new table echo "Done!<br>\n"; } echo "Deleting $key from old table...."; mysql_query("DELETE FROM users WHERE 1='$key'"); echo "Done!<br>\n"; /* this deletes the user from the table users regardless if you selected to accept or deny. If you chose to accept the user, it runs the query above first and then this one */ } } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427214 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 wow thanks so much... you rock Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427439 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 I ran into a problem... when you click accept i want it to go to a table called 'register' .. so i replaced 'users' with register but it won't move or delete the username. Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427447 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 And it won't delete a username unless they all are selected to be deleted Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427462 Share on other sites More sharing options...
Link Posted January 1, 2008 Share Posted January 1, 2008 It depends on what you are moving. Can you give the specs of the two tables? Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427469 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 Can we focus on the deleting atm then the moving Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427477 Share on other sites More sharing options...
Link Posted January 1, 2008 Share Posted January 1, 2008 I mean, I could complete the script for you if I knew the table information, so I could know how to identify entries and such. Is '1' the name of a column in your table? Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427478 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 THis is my table... i'm not using anything besides username, password and email in it. Although, everytime I try to delete anything else it messes the whole thing up. ps: yes, 1 is a column in my table CREATE TABLE `users` ( `ID` int(11) NOT NULL auto_increment, `Username` varchar(255) NOT NULL, `Password` varchar(255) NOT NULL, `Temp_pass` varchar(55) default NULL, `Temp_pass_active` tinyint(1) NOT NULL default '0', `Email` varchar(255) NOT NULL, `Active` int(11) NOT NULL default '0', `Level_access` int(11) NOT NULL default '2', `Random_key` varchar(32) default NULL, PRIMARY KEY (`ID`), UNIQUE KEY `Username` (`Username`), UNIQUE KEY `Email` (`Email`) ) ENGINE=MyISAM; Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427492 Share on other sites More sharing options...
Link Posted January 1, 2008 Share Posted January 1, 2008 This should work as long as you replace *NEW_TABLE* with the new table name, and you have the fields Username, Password, and Email created for that new table. Let me know if you have problems. <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("login", $con); if(!isset($_POST['submit'])) { if (!$result = mysql_query("SELECT * FROM users")) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['ID']; echo "</b> "; echo "<input type=radio name=\"". $row['ID'] ."\" value=1>Accept<br>"; echo " <input type=radio name=\"". $row['ID'] ."\" value=2>Decline<br>"; echo " <br />"; } echo "<br><input type=submit name=submit value=Submit>"; } else { foreach($_POST as $key => $val){ if($val == 1) { $info = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE ID='".$key."'")); echo "Inserting $key into new table...."; mysql_query("INSERT INTO *NEW_TABLE* SET Username='".$info['Username']."', Password='".$info['Password']."', Email='".$info['Email']."'"); // Moves user to new table echo "Done!<br>\n"; } echo "Deleting $key from old table...."; mysql_query("DELETE FROM users WHERE ID='".$key."'"); echo "Done!<br>\n"; /* this deletes the user from the table users regardless if you selected to accept or deny. If you chose to accept the user, it runs the query above first and then this one */ } } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427501 Share on other sites More sharing options...
Northern Flame Posted January 1, 2008 Share Posted January 1, 2008 so you have 2 tables? users is where they are if they are pending and register is where they are if you have accepted them? Are you sure you edited the SQL information in my script to match your SQL info? Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427502 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 users is where they are pending yes, and register is where they should be sent... I don't know if I edited it right, i'll post it back. It should all be right. Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427506 Share on other sites More sharing options...
trq Posted January 1, 2008 Share Posted January 1, 2008 A better desgn might simply be to have a filed in your users table called pending, make it an int. You could there store a 1 in this filed for all pending accounts or update it to a 0 for all active accounts. No need for two seperate tables at all. Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427507 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 This is my code currently <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("login", $con); if(!isset($_POST['submit'])){ if ( !$result = mysql_query("SELECT * FROM users") ) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['1']; echo "</b> "; echo "<input type=radio name=\"". $row['1'] ."\" value=1>Accept<br>"; echo " <input type=radio name=\"". $row['1'] ."\" value=2>Decline<br>"; echo " <br />"; } echo "<br><input type=submit name=submit value=Submit>"; } else{ foreach($_POST as $key => $val){ if($val == 1){ echo "Inserting $key into new table...."; mysql_query("INSERT INTO new_table(register) VALUES('$key')"); // Moves user to new table echo "Done!<br>\n"; } echo "Deleting $key from old table...."; mysql_query("DELETE FROM users WHERE 1='$key'"); echo "Done!<br>\n"; /* this deletes the user from the table users regardless if you selected to accept or deny. If you chose to accept the user, it runs the query above first and then this one */ } } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427508 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 True thorpe, but i think it would be much easier to have two tables considering i'm rather newbie, and it would probably take more code to sort through it for logging in. Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427510 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 Okay Link, yours worked Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427513 Share on other sites More sharing options...
Northern Flame Posted January 1, 2008 Share Posted January 1, 2008 heres your mistake: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("login", $con); if(!isset($_POST['submit'])){ if ( !$result = mysql_query("SELECT * FROM users") ) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['1']; echo "</b> "; echo "<input type=radio name=\"". $row['1'] ."\" value=1>Accept<br>"; echo " <input type=radio name=\"". $row['1'] ."\" value=2>Decline<br>"; echo " <br />"; } echo "<br><input type=submit name=submit value=Submit>"; } else{ foreach($_POST as $key => $val){ if($val == 1){ echo "Inserting $key into new table...."; mysql_query("INSERT INTO register VALUES('$key')"); // This is the fix echo "Done!<br>\n"; } echo "Deleting $key from old table...."; mysql_query("DELETE FROM users WHERE 1='$key'"); echo "Done!<br>\n"; /* this deletes the user from the table users regardless if you selected to accept or deny. If you chose to accept the user, it runs the query above first and then this one */ } } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427515 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 Now I just gotta make a register for this -.- Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427516 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 Thanks Guys =-D ... gonna make a new forum a bit later on registering Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427517 Share on other sites More sharing options...
Link Posted January 1, 2008 Share Posted January 1, 2008 He's right though, you could alleviate the need for two tables with another value. It would only add one more conditional to your login script, and I would be more than happy to help you with it. Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427518 Share on other sites More sharing options...
php? Posted January 1, 2008 Author Share Posted January 1, 2008 ughh so much more work -,- Quote Link to comment https://forums.phpfreaks.com/topic/83946-solved-deleting-and-moving/#findComment-427528 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.