Form doesn't update

How would I echo the query?


<?php

if (isset($_POST['Submit'])) {

        $active_v = mysql_real_escape_string($_POST['active']);

       echo '<p>',( empty($_POST['active']) ? 'Active is empty': 'Active is not empty' ),'</p>'; // test whether the value is empty or not

        if ((!empty($_POST['active']))) {

                echo "<p>UPDATE users SET active = '$active_v' WHERE id='1'</p>";

                // mysql_query("UPDATE users SET active = '$active_v' WHERE id='1'") or die("MYSQL is indeed gay: ".mysql_error());

        }

                echo 'Status updated'; 

    } //If Submit



?>

Until you put a start form tag in your HTML and set the method to POST you guys wont get very far with this thread. :wink:


<form method="POST" action="yourscriptname.php">

Despite the fact that the form needs to have a starting tag, if the method isnt defined it will default to GET. The OP put you all on the wrong track by stating that the echo message was output when it clearly wasnt with the code he posted.

It gave me this :
Active is not empty

UPDATE users SET active = ‘test’ WHERE id=‘1’

What was the result of that running it on the command prompt in MySQL?

Command prompt O.o

It just gave me this on website.

Still need help with this, please anyone!

Run the SQL statement outside PHP to see if there are any actual issues with the query itself.

Did you add the opening <form> tag as I said in post 23 ?

Post your latest code so we can see what youve got now !

My current code :


<form name="active" action="_view.php" method="post">

<?php

if (isset($_POST['Submit'])) {

        $active_v = mysql_real_escape_string($_POST['active']);

       echo '<p>',( empty($_POST['active']) ? 'Active is empty': 'Active is not empty' ),'</p>'; // test whether the value is empty or not

        if ((!empty($_POST['active']))) {

                echo "<p>UPDATE users SET active = '$active_v' WHERE id='1'</p>";

                // mysql_query("UPDATE users SET active = '$active_v' WHERE id='1'") or die("MYSQL is indeed gay: ".mysql_error());

        }

                echo 'Status updated'; 

    } //If Submit



?>


		<tr>
    <td><strong>Activate/Suspend :</strong></td>
    <td>
<input size="250" class="booter" value="<? echo $active; ?>" name="active" type="text" id="active">Fill in "activated" to active, other text for suspend.
</td>
  </tr>
<tr>
<td>
</td>
<td>
<input type="submit" class="booter" name="Submit" value="Update">
</td>
</form></tr>
	</table>

Couple of points

You’ve called your form and one input by the same name ‘active’, although this wont be the cause of your problem you should change one, else you may encouter problems later on down the line. I suggest you change the form name to ‘activeform’ to differentiate the two. There are other HTML errors (no start table tag etc.)

Your echo ‘table updated’ is in the wrong place, it will echo on $_post[‘submit’] being set,even if the query fails.

Your update statement is commented out !!

Where are you connecting to the database ? The command mysql_real_escape_string requires a database connection, if you dont have a connection that string will return null.

Try this code


<?php
// connect to your database here 
//
if (isset($_POST['Submit'])) {
    $active_v = mysql_real_escape_string($_POST['active']);
    if (!empty($_POST['active'])) {
     echo "<p>UPDATE users SET active = '$active_v' WHERE id='1'</p>";
        $result = mysql_query("UPDATE users 
        SET active = '$active_v' 
          WHERE id='1'
         ");
    if (!$result) {
       die('NO RESULT: ' . mysql_error());
    }else{
       echo 'Query ran OK'; 
    }
  }else{
    echo '$_POST is empty':
    die;
  }
} //If Submit
?>
<form name="activetable" action="_test.php" method="post">
<table>
 <tr>
     <td><strong>Activate/Suspend :</strong></td>
     <td>
   <input size="50" class="booter" value="<?php echo $active; ?>" name="active" type="text" id="active">Fill in "activated" to active, other text for suspend.
  </td>
   </tr>
 <tr>
  <td></td>
  <td>
   <input type="submit" class="booter" name="Submit" value="Update">
  </td>
 </tr>
</table>
</form>

Tell us what response you get, if it still isnt working then post the structure of your Users table along with any error messages.

Well, Ive tried it but it returned to _view.php

I got an username list, u can click on it.
Then you see for example _view.php?user=test

But when I submit form, it returns to _view.php
and nothing happens.

When I remove _view.php from action, and do it, then I get this message :
UPDATE users SET active = ‘activatedaaa’ WHERE id=‘1’
Query ran OK

But it didn’t change.

And my table is

CREATE TABLE IF NOT EXISTS users (
id int(5) NOT NULL auto_increment,
username varchar(50) NOT NULL,
password varchar(32) NOT NULL,
paypal varchar(50) NOT NULL,
trans_id varchar(30) NOT NULL,
staff varchar(30) NOT NULL,
months varchar(10) NOT NULL,
active text NOT NULL,
lastactive int(20) NOT NULL,
userip varchar(32) NOT NULL,
loginip varchar(322) NOT NULL,
nextboot int(32) NOT NULL,
terms varchar(3) NOT NULL default ‘no’,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=326 ;

Then it would seem that you dont have a database entry with ID = 1 ?

How would I fix it?

First thing to do is verify that what Im saying is true, I can only guess as Ive not seen the contents of your table.

In myPHPadmin use this command to list the table


SELECT * FROM users 
ORDER BY id ASC
LIMIT 20;

Post the results here

Nevermind got it finally thanks for all the help!

Took long but we got there XD

:slight_smile:

When comparing against an integer in SQL the proper form is x = 1 NOT x = ‘1’. You should change that. Also, int(5) should probably be MEDIUMINT UNSIGNED or BIGINT UNSIGNED, int(5) does not do what you think it does. Your working with PAYMENTS and couldn’t figure out there was no row with a primary key of one… I hope its not for production purposes and if it is god have mercy on your client. What is the domain of this site…

I’m not one to normally say things like that but incorrect code when dealing with payments makes you and your client susceptible to legal issues and hacked purchases. Payments aren’t really something you should be dealing with unless you know what you are doing because getting things wrong, even when using a third party gateway can have very negative consequences for all parties involved.