0

I have a small issue with updating my database upon a submit.

I have the following in my Database: a varchar called iUserCash.

Upon login I would like to edit this row in my database table.

The html looks like this:

<form method="post">
            <table class="sign_up_form" align="center" width="30%" border="0">
            <tr>
             <td>
                <input type="text" name="cashBalance" placeholder="Nye beløb"/>
            </td>
            <td>
                <button type="submit" name="btn-update" class="betting-btn">OPDATER</button>
            </td>
            <td>
            </tr>
            <tr>
            </tr>
            </table>
            </form>

And my sql looks like this:

session_start();
include_once 'controllers/dbConnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM oneusers WHERE iUserId=".$_SESSION['user']);

$userRow=mysql_fetch_array($res);

if(isset($_POST['btn-update']))
{
 $ucash = mysql_real_escape_string($_POST['cashBalance']);

 if(mysql_query("UPDATE oneusers SET iUserCash = '$ucash' WHERE iUserId='$res'"))
 {
?>
        <script>alert('successfully registered ');</script>
        <?php
 }
 else
 {
  ?>
        <script>alert('error while registering you...');</script>
        <?php
 }
}

It returns the success message just fine, but it just doesnt update anything. Can anyone tell me what I am doing wrong? :) Thanks in advance.

  • 1
    `$res` isn't a value, it's a resource, so `iUserId='$res'` makes no sense. – Jonnix Jun 10 '16 at 08:17
  • in form pass the iUserId in hidden filed and use it in update query – JYoThI Jun 10 '16 at 08:21
  • The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MYSQLi or PDO_MySQL extensions. prepared statement – JYoThI Jun 10 '16 at 08:22

1 Answers1

1

you have a error at

mysql_query("UPDATE oneusers SET iUserCash = '$ucash' WHERE iUserId='$res'")

you are using $res for iUserId but it's a db resource...

it seems that, $_SESSION['user'] is the id that you need in query... so try it like

    mysql_query("UPDATE oneusers SET iUserCash = '$ucash' WHERE iUserId=" . $_SESSION['user']);
tanaydin
  • 5,171
  • 28
  • 45
  • 1
    why not this WHERE iUserId='" . $_SESSION['user'] . "' – JYoThI Jun 10 '16 at 08:24
  • how do you know $_SESSION['user'] is id ? I'm just predicting. – tanaydin Jun 10 '16 at 08:25
  • 1
    did u seen that same table 'oneusers' and same coloumn 'iUserId' name he used in both query that's why i am telling that – JYoThI Jun 10 '16 at 08:29
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Jun 10 '16 at 08:30
  • yeah @jothi I saw now what you mean... you are right... updating answer. – tanaydin Jun 10 '16 at 08:31
  • This fixed it! :) Thanks a lot! – Patrick Ziebell Thøgersen Jun 10 '16 at 08:34