pcw Posted March 30, 2009 Share Posted March 30, 2009 Hi, I have written this script to allo me to approve or disapprove members. When the approve or disapprove button is clicked, the approved should be set to yes or no in the database, however it is not writing anything. This is what I have got <?php include("data/required.php"); if($_POST=="approve") { mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE profiles SET approved = 'yes' WHERE username='$username'"); }; if($_POST=="disapprove") { mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE profiles SET approved = 'no' WHERE username='$username'"); }; mysql_connect("$hostname", $dbuser, $dbpass) or die(mysql_error()); mysql_select_db("$db") or die(mysql_error()); $query = "SELECT username, firstname, surname, approved FROM profiles"; $result=mysql_query($query) or die(mysql_error()."<br /><br />".$query); echo "<table width=100% border=1> <tr class=style8> <td align=center bgcolor='#0066FF'><b>Username</b></td> <td align=center bgcolor='#0066FF'><b>Name</b></td> <td align=center bgcolor='#0066FF'><b>Surname</b></td> <td align=center bgcolor='#0066FF'><b>Approved</b></td> <td align=center bgcolor='#0066FF'><b>Approve</b></td> <td align=center bgcolor='#0066FF'><b>Disapprove</b></td> </tr>"; while($row = mysql_fetch_array($result)){ print "<tr> <td align=center bgcolor='#0099FF'>".$row['username']."</td> <td align=center bgcolor='#0099FF'>".$row['firstname']."</td> <td align=center bgcolor='#0099FF'>".$row['surname']."</td> <td align=center bgcolor='#0099FF'>".$row['approved']."</td> <td align=center bgcolor='#0099FF'> <form method='POST' action='$PHP_SELF'> <input type='hidden' name='username' value='$username'> <input type='submit' name='approve' value='Approve'> </form></td> <td align=center bgcolor='#0099FF'> <form method='POST' action='$PHP_SELF'> <input type='hidden' name='username' value='$username'> <input type='submit' name='disapprove' value='Disapprove'> </form></td> </tr>"; } echo "</table></center>"; ?> Thanks Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/ Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 I have a feeling you should read through this article http://www.w3schools.com/php/php_post.asp Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797004 Share on other sites More sharing options...
will35010 Posted March 30, 2009 Share Posted March 30, 2009 I'm very new to php, but would it make a difference that your form is after your mysql code? Maybe it's not defined yet. Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797006 Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 will, it doesn't matter. When he posts the form, it begins processing from the top down. Though he could do his procession even after the form if he desired and the only difference would be he would be unable to change the header and anything he output would need to also come out after the form. PHP is executed before anything is ever sent to the client/user/browser. Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797009 Share on other sites More sharing options...
pcw Posted March 30, 2009 Author Share Posted March 30, 2009 Brian, I have had a look through this and dont see how that will solve my problem. Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797011 Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 umm, the problem is that you don't understand how to use $_POST. Assuming you understand what a string is and what an array is, $_POST is an array. $_POST == "whatever" is a string comparison and will always return false unless you've oddly changed set $_POST to a string manually. what you are looking at is $_POST['approve'] and $_POST['disapprove'] hope that helps a little Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797018 Share on other sites More sharing options...
savagenoob Posted March 30, 2009 Share Posted March 30, 2009 maybe try this.... <?php if(isset($_POST['approve'])) { mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE profiles SET approved = 'yes' WHERE username='$username'"); } elseif(isset($_POST['disapprove'])) { mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE profiles SET approved = 'no' WHERE username='$username'"); } ?> Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797023 Share on other sites More sharing options...
pcw Posted March 30, 2009 Author Share Posted March 30, 2009 Thanks guys, but neither is working, I dont understand the prob here! Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797040 Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 not working, how so? Do you receive an error? Change mysql_query("UPDATE profiles SET approved = 'yes' WHERE username='$username'"); to mysql_query("UPDATE profiles SET approved = 'yes' WHERE username='$username'") or die(mysql_error()); and then click on the approve link and see what happens. Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797044 Share on other sites More sharing options...
pcw Posted March 30, 2009 Author Share Posted March 30, 2009 Hi Brian, no error is returned. When I click approve or disapprove, the page is refreshed but nothing happens to the database, and the "Approved" column on the page, remains blank Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797055 Share on other sites More sharing options...
savagenoob Posted March 30, 2009 Share Posted March 30, 2009 This should work as far as I can tell... here is the complete code as I would write it... <?php if(isset($_POST['approve'])) { mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE profiles SET approved = 'yes' WHERE username='$username'") or die(mysql_error()); } elseif(isset($_POST['disapprove'])) { mysql_connect('localhost', $dbuser, $dbpass) or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); mysql_query("UPDATE profiles SET approved = 'no' WHERE username='$username'"); } else { mysql_connect("$hostname", $dbuser, $dbpass) or die(mysql_error()); mysql_select_db("$db") or die(mysql_error()); $query = "SELECT username, firstname, surname, approved FROM profiles"; $result=mysql_query($query) or die(mysql_error()."<br /><br />".$query); echo "<table width=100% border=1> <tr class=style8> <td align=center bgcolor='#0066FF'><b>Username</b></td> <td align=center bgcolor='#0066FF'><b>Name</b></td> <td align=center bgcolor='#0066FF'><b>Surname</b></td> <td align=center bgcolor='#0066FF'><b>Approved</b></td> <td align=center bgcolor='#0066FF'><b>Approve</b></td> <td align=center bgcolor='#0066FF'><b>Disapprove</b></td> </tr>"; while($row = mysql_fetch_array($result)){ print "<tr> <td align=center bgcolor='#0099FF'>".$row['username']."</td> <td align=center bgcolor='#0099FF'>".$row['firstname']."</td> <td align=center bgcolor='#0099FF'>".$row['surname']."</td> <td align=center bgcolor='#0099FF'>".$row['approved']."</td> <td align=center bgcolor='#0099FF'> <form method='POST' action='$PHP_SELF'> <input type='hidden' name='username' value='$username'> <input type='submit' name='approve' value='Approve'> </form></td> <td align=center bgcolor='#0099FF'> <form method='POST' action='$PHP_SELF'> <input type='hidden' name='username' value='$username'> <input type='submit' name='disapprove' value='Disapprove'> </form></td> </tr>"; } echo "</table></center>"; } ?> Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797066 Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 place this at the top of the page just below <?php error_reporting(E_ALL); Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797069 Share on other sites More sharing options...
savagenoob Posted March 30, 2009 Share Posted March 30, 2009 How bout this... change both action='$PHP_SELF' to action='$_SERVER['PHP_SELF']' Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797079 Share on other sites More sharing options...
pcw Posted March 30, 2009 Author Share Posted March 30, 2009 Doh! It was me being dopey this fixed the problem <input type='hidden' name='username' value=".$row['username']."> Thanks for all yr help guys Link to comment https://forums.phpfreaks.com/topic/151780-solved-mysql-update-problem/#findComment-797080 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.