Jump to content

[SOLVED] Wrong Text


php?

Recommended Posts

Now what's the problem?

[uPDATED PROBLEM]

 

*sigh* Well, according to the script below, it should display "Username ($key) is being deleted...done", or "Username ($key) is being accepted...done"

 

But instead it displays something like this: "11 is being deleted" or "11 is being accepted "

 

For some reason $key is being set to the ID number field of the table... not the username field

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
      die('Could not connect: ' . mysql_error());
}

mysql_select_db("mylogin", $con);

$a = mysql_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"." <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 "$key is being accepted...";
                  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 "$key is being deleted...";
            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);
}

?>

 

 

Link to comment
Share on other sites

you cant use this line  foreach($_POST as $key => $val) because there are lots of post that you don't need eg.. the submit ..button it will be include in your loop

an what do you mean by For some reason $key is being set wrong, and I have no idea why the "submit" is there...

Link to comment
Share on other sites

<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
      die('Could not connect: ' . mysql_error());
}

mysql_select_db("mylogin", $con);

$a = mysql_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_s[".$row['ID']."]' value=1>";
		echo "Accept"." <input type=radio name='row_s[".$row['ID']."]' value=2>Decline<br>";
            echo " <br />";
      }
      echo "<br><input type=submit name=submit value=Submit>";
} else {
      foreach($_POST['row_s'] as $key => $val){
//  print_r($_POST['row_s']);//ro check if yuo get thevalues of chk boxes
            if($val == 1) {
                  $info = mysql_fetch_array(mysql_query("SELECT * FROM pending WHERE ID='".$key."'"));
                  echo "$key is being accepted...";
                  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 "$key is being deleted...";
            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);
}

?>

maybe you could try something like that

Link to comment
Share on other sites

      foreach($_POST['row_s'] as $key => $val){
         $info = mysql_fetch_array(mysql_query("SELECT * FROM pending WHERE ID='".$key."'"));
            if($val == 1) {
                  echo "{$info['Username']} is being accepted...";
                  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 "{$info['Username']} is being deleted...";
            mysql_query("DELETE FROM pending WHERE ID='".$key."'");
            echo "done <br>\n";

that?

Link to comment
Share on other sites

Alrighty there Mr. Clarification,

 

After I click decline on a username (lets call the username "Mr. Clarification"), it deletes from the database. After it deletes it should display a message that says: 'Mr. Clarification has been deleted...'. In the code $key should be the username. But... instead it gives me a message that says: '18 has been deleted.... And 18 is the ID code of the table that the username should be taken from.

Link to comment
Share on other sites

lol alright,

try this:

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
      die('Could not connect: ' . mysql_error());
}

mysql_select_db("mylogin", $con);

$a = mysql_query("SELECT * FROM `pending`");
$b = mysql_num_rows($a);

if($b == '0')
{
echo 'No pending requests at the moment.';
}
else
{
if(!isset($_POST)) {
      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"." <input type=radio name=\"". 

$row['ID'] ."\" value=2>Decline<br>";
            echo " <br />";
      }
      echo "<br><input type=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 "$val is being accepted...";
                  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 "$key is being deleted...";
            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);
}

?>

Link to comment
Share on other sites

u have to use the for loop like this

foreach($_POST['row_s'] as $key => $val)

 

or

in the javascript when the user checks any radio button put a cooresponding value in a hidden variable and in the php check whether if(isset($_POST['checkradio']) && $_POST['checkradio']!='')

{

now do the action ...

}

 

 

in any case as ur making the for-element like ...$rows["ID"] i think in the db it is a integer .......

if u print $key it will u like ...18,1,16...like that (or) make sure either u use a different col-name for name or use $val in php

Link to comment
Share on other sites

replace the foreach with this:

 

      foreach($_POST['row_s'] as $key => $val){
         $info = mysql_fetch_array(mysql_query("SELECT * FROM pending WHERE ID='".$key."'"));
            if($val == 1) {
                  echo "{$info['Username']} is being accepted...";
                  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 "{$info['Username']} is being deleted...";
            mysql_query("DELETE FROM pending WHERE Username='".$info['Username']."'");
            echo "done <br>\n";

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.