0

I have already build a functioning login page which redirects the user the index.php file. From previous help, I was able to get the wage and display it on the page depending on which user logs in. This is the table users in the database user_registration

user_id    username   password    email                 wage
1          johnsmith  jsmith99    jsmith@gmail.com      100
2          davidscott dscott95    davidscott@gmail.com  90

The part i am stuck on is creating a functioning form that the user can update their wage to the sql database.

Can someone please help me with the php code? This is the form i already have in place:

<form id="change-wage" action="update.php" method="post">
  <input type="text" id="new_wage" name="new_wage">
  <input type="button" value="Save">
</form>

EDIT: this is the code, The aim of it is that the user can update the wage value in the table by filling in the textbox and pressing submit. any Ideas how i can acieve this?>

<?php  //CHANGING THE WAGE
$username = '$_SESSION['MM_Username'];';
if (isset($_POST['submit'])){
        $wage = $_POST['wage-new'];
        //connect to server
        mysql_connect ("localhost","root","") or die ("Could not connect");
        mysql_select_db("user_registration") or die ("Could not connect to the database");

    mysql_query ("UPDATE users SET wage='$wage' WHERE username = '$username'")     or die ("Could not update");
}


    ?>
Sam
  • 490
  • 1
  • 5
  • 22
  • 8
    Questions asking for code must **demonstrate a minimal understanding of the problem being solved**. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). – John Conde Apr 08 '14 at 15:07
  • ok, i have included the php code that doesn't work, im not sure what is wrong, as the webpage doesn't return any results – Sam Apr 08 '14 at 15:32
  • Plaintext passwords, huh? You should change that. – The Blue Dog Apr 08 '14 at 16:41
  • haha yeah, its just for the moment whilst i set it up - it will be encrypted when i finish :3 – Sam Apr 08 '14 at 16:48
  • your post variable name is new_wage not wage-new. maybe the issue – stephenbayer Apr 08 '14 at 16:53
  • @SAMTHEMAN999: Cool. Will you stop using [**deprecated**](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) `mysql_*` functions before you hash the passwords, or after? – The Blue Dog Apr 08 '14 at 16:54
  • yes, this is also what ive been confused about, ive seen on many posts that you should stop using this function, what i want to know is WHY and HOW?? (as you could have guessed im still learning with php) – Sam Apr 08 '14 at 16:58
  • @SAMTHEMAN999: That's why I posted a [**LINK**](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – The Blue Dog Apr 08 '14 at 17:03
  • ok thanks, so this means that i should replace the mysql_* functions with mysqli_ functions – Sam Apr 08 '14 at 17:07

2 Answers2

1

I wont give you the code unless you demonstrate as previous commentator said. However I will give you a an overview so you can work at it your self.

update.php

Check if your is logged in.

if TRUE, continue.

get the new wage from the form

$new_wage = $_POST['new_wage'];

Be sure to validate and clean the $new_wage variable.

Next stage assumes your using PDO

$params = array($new_wage, $logged_in_user_id);

$update = "UPDATE user_registration SET wage=? WHERE user_id=?";

$pdo->prepare($update);
$pdo->execute($params);

Syed Hussim
  • 720
  • 1
  • 4
  • 16
0

First of all if you are using session variables make sure you start the session -session_start();

$username = '$_SESSION['MM_Username'];';

should be

$username = $_SESSION['MM_Username']; (without single quotes)

$wage = $_POST['wage-new'];

should be

$wage = $_POST['new_wage']; as you named it in your html file

you are selecting database user_registation and I assume it should be user_registration

And last, think about switching to PDO or mysqli.

Jerko W. Tisler
  • 996
  • 9
  • 29