Jump to content

[SOLVED] Mysql UPDATE problem


pcw

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.