Not Updating In Database

I’m sure it is just a small little mistake, but I can not find it. The page shows up fine, just when I hit submit, it does not post in the database.

<?php 
session_start();
include ("config.php");
if (!isset($_SESSION['logged_in'])) {
    if (isset($_POST[update])) {
        $email = addslashes(htmlspecialchars($_POST['email']));
        $habbo = addslashes(htmlspecialchars($_POST['habbo']));
        $password = md5($_POST['password']);
        $upd = mysql_query("UPDATE `users` SET email = '" . $email . "', habbo = '" . $habbo . "', password = '" . $password ."' WHERE username = '$username'");
        echo "Profile updated!";
    } 
} else {
    echo "<form method='post' action='edit.php'>
    Email:<br>
    <input type='text' name='email' size='20'><br>
    Habbo name:<br>
    <input type='text' name='habbo' size='20'><br>
    Password:<br>
    <input type='password' name='password' size='20'><br>
    <input type='submit' name='update' value='Update'>";
}
?>

I have tried making the action <?= $_SERVER[‘PHP_SELF’] ?>, and every little thing I can think of. And it still just is not working.

try turning on error reporting to see what happens

put this function in ur page:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

I put it right underneath the <?php opening tag, and it doesn’t show anything.

This used to happen with me when I started coding.
Generally the problem is with the where clause.

$query="
    UPDATE
        `users`
    SET
          email = '$email'
        , habbo = '$habbo'
        , password = '$password'
    WHERE
        username = '$username'
";

$upd = mysql_query($query);

If still no luck, then try echoing out the query and running direct against MySQL to see if you have any errors with your SQL

$query="
    UPDATE
        `users`
    SET
          email = '$email'
        , habbo = '$habbo'
        , password = '$password'
    WHERE
        username = '$username'
";

echo "&lt;p&gt;SQL: $query&lt;/p&gt;";

$upd = mysql_query($query);

Where dose $username come from?

If you have it set as a session then you will need to use


$_SESSION['username']

I have made the changes. But, it still isn’t working. I have changed the isset($_SESSION[‘username’]) but, I am just not sure why it is not working.

bobero, the variable $username isn’t set anywhere. If you echo out your query, you’ll probably see an empty username field in the where clause.

Replace


$upd = mysql_query("UPDATE `users` SET email = '" . $email . "', habbo = '" . $habbo . "', password = '" . $password ."' WHERE username = '$username'");

With


echo "UPDATE `users` SET email = '" . $email . "', habbo = '" . $habbo . "', password = '" . $password ."' WHERE username = '$username'"

Or, do an:


echo $username;

To see if that variable has been set because by the looks of it that variable has not been set.

Have you made a form with a username field?

Strange to see no one has mentioned [fphp]mysql_real_escape_string[/fphp] or [fphp]mysql_error[/fphp] yet. :slight_smile:

try this code

$email = addslashes(htmlspecialchars($_POST['email']));
        $habbo = addslashes(htmlspecialchars($_POST['habbo']));
        $password = md5($_POST['password']);
        $username=$_SESSION['username'];
        $upd = mysql_query("UPDATE `users` SET email = '" . $email . "', habbo = '" . $habbo . "', password = '" . $password ."' WHERE username = '$username'");
        echo "Profile updated!";