php? Posted January 2, 2008 Share Posted January 2, 2008 Hi, title basically explains it. How do you i make the script below display "No Registrations Pending" if there are 0 usernames in the database <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mylogin", $con); if(!isset($_POST['submit'])) { if (!$result = mysql_query("SELECT * FROM pending")) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['Username']; 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 pending WHERE ID='".$key."'")); echo "Inserting $key into new table...."; mysql_query("INSERT INTO accepted 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 pending 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 Share on other sites More sharing options...
duclet Posted January 2, 2008 Share Posted January 2, 2008 Before you retrieve the rows from the sql query, use mysqli_num_rows (or something like that, I don't remember the function name) to see if there are any pending users. Follow that with an if-else statement for the possible options: if there is 0 pending or if there is at least 1. Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 Thats helps some... but sketchy Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 Anyone have anymore suggestions ??? I'm on the rather newbie side. Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted January 2, 2008 Share Posted January 2, 2008 heres the code to what duclet was talkin bout ..i think ^^ <?php $a = myql_query("SELECT * FROM `pending`"); $b = mysql_num_rows($a) if($b == '0') { echo 'No pending requests at the moment.'; } else { //anythin below here, is the code that will display pendin requests, if there are any } ?> Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted January 2, 2008 Share Posted January 2, 2008 replace <?php $b = mysql_num_rows($a) ?> with <?php $b = mysql_num_rows($a); ?> forgot the stupid semi colon Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 Where would i put that in my code ??? Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 2, 2008 Share Posted January 2, 2008 Where would i put that in my code ??? where you want to out put that result Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted January 2, 2008 Share Posted January 2, 2008 <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mylogin", $con); $a = myql_query("SELECT * FROM `pending`"); $b = mysql_num_rows($a); if($b == '0') { echo 'No pending requests at the moment.'; } else { if(!isset($_POST['submit'])) { if (!$result = mysql_query("SELECT * FROM pending")) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['Username']; 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 pending WHERE ID='".$key."'")); echo "Inserting $key into new table...."; mysql_query("INSERT INTO accepted 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 pending 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); } ?> see if it works.. Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 Fatal error: Call to undefined function myql_query() in C:\xampp\htdocs\mylogin\admin.php on line 9 And teng i want it where the usernames would normally be displayed Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 2, 2008 Share Posted January 2, 2008 error is obvious myql_query() should be mysql_query() Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 2, 2008 Share Posted January 2, 2008 if (!$result = mysql_query("SELECT * FROM pending")) { i believe this will always return false because you only setting data Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 2, 2008 Share Posted January 2, 2008 <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mylogin", $con); if(!isset($_POST['submit'])) { $result = mysql_query("SELECT * FROM pending") if (!$result) { die('MySQL Error: ' . mysql_error()); } echo "<form method=\"POST\""; while($row = mysql_fetch_array($result)) { echo "<b>" . $row['Username']; 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 pending WHERE ID='".$key."'")); echo "Inserting $key into new table...."; mysql_query("INSERT INTO accepted 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 pending 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); ?> try Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 No it worked hehe =D just my mentally challenged error.. thanks again guys =O Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 One more thing.. after i delete a username from the database when it displays the message it comes up with this... Deleting 11 from old table....Done! Deleting submit from old table....Done! the name is username... dunno where the 11 and submit came from Quote Link to comment Share on other sites More sharing options...
interpim Posted January 2, 2008 Share Posted January 2, 2008 11 is most likely your unique ID number assigned by your Database... Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 Its setting the $Key wrongly.. idk why Quote Link to comment Share on other sites More sharing options...
deadimp Posted January 2, 2008 Share Posted January 2, 2008 The "11" and "submit" comes from the your POST data. Those are the names for the fields on your form, the radio array and your submit button. You don't sanitize your input at all, and that's extremely insecure. Someone could inject any type of command on their side and it could be executed on your MySQL server. Making the loop based off of something other than pure POST data would be best. You need to better define your application's limits. Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 And so is this forum... posting comments like that on a help forum aren't relatively helpful. Quote Link to comment Share on other sites More sharing options...
php? Posted January 2, 2008 Author Share Posted January 2, 2008 And btw... i'm getting the basics done before the security Quote Link to comment Share on other sites More sharing options...
deadimp Posted January 2, 2008 Share Posted January 2, 2008 Well, please keep that in mind for later. Quote Link to comment 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.