Jump to content

[SOLVED] Deleting and Moving


php?

Recommended Posts

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'");

 

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
?>

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.