user3311898
user3311898

Reputation: 113

PHP profile update page MySql Error

Can anyone see the error in this code as the code is only giving me back :

the name does not exist

It was all working fine now it does not.

If anyone can spot it please and correct me as I am still new to this.

<?php
    // see if the form has been completed
    include_once("php_includes/check_login_status.php");
    include_once("php_includes/db_conx.php");
    // Initialize any variables that the page might echo
    $username = "";
    $firstname = "";
    $surname = "";
    $gender = "Male";
    $country = "";
    $weight = "";
    $height = "";

    if(isset($_GET["u"])){
        $username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
    } 

    $sql = "SELECT * FROM users WHERE username='$username' AND activated='1' LIMIT 1";
    $user_query = mysqli_query($db_conx, $sql);

    // check if the user exists in the database
    while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
        $username = $row ["username"];
        $firstname = $row["firstname"];
        $surname = $row["surname"];
        $weight = $row["weight"];
        $height = $row["height"];
        $email = $row["email"];
        $gender = $row ["gender"];
        }
    if (isset($_POST['submit'])){
        $username = $_POST['username'];
        $firstname = $_POST['firstname'];
        $surname = $_POST['surname'];
        $weight = $_POST['weight'];
        $height = $_POST['height'];
        $email = $_POST['email'];
        $gender = $_POST['gender'];

        mysql_connect ("host","****","*****"); mysql_select_db('db_k1003140');
        // check if that user exist 
        $exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $username . "'") or die ("query cant connect");    
        if (mysql_num_rows ($exists) != 0) {
        // update the description in the database       
            mysql_query("UPDATE users SET firstname='$firstname', surname='$surname', weight='$weight', height='$height' WHERE username='$username'") or die ("update could not be applied");
            echo "successful";
        } else echo "the name does not exist";  
     }
?>  

Here is the HTML :

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Profile Update: <?php echo $u; ?></title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="js/main.js"></script>
    <script src="js/javascript.js"></script>
    <script src="js/ajax.js"></script>
    <style type="text/css">
        #updateform{
        margin-top:24px;    
        }
        #updateform > div {
            margin-top: 12px;   
        }
        #updateform > input {
            width: 200px;
            padding: 3px;
            background: #F3F9DD;
        }       
</style>

</head>
<body>
    <?php include_once("template_pageTop.php"); ?>
    <div id="pageMiddle">       
    <div id="usernamecss"> Username: <?php echo $username; ?></div>
    <form action="update.php" method="POST" id="updateform">
    <div>

    <div>First Name: </div>
    <input id="firstname" type="text" name="firstname" value="<?php echo $firstname?>" maxlength="16">
    <div>Surname: </div>
    <input id="surname" type="text" name="surname" value="<?php echo $surname?>" maxlength="16">
    <div>Weight: </div>
    <input id="weight" type="text" name="weight" value="<?php echo $weight?>" >
    <div>Height: </div>
    <input id="height" type="text" name="height" value="<?php echo $height?>" >


    <p> <input type="submit" name="submit" id="submit" value="Update Description"></p>

    <a href="user.php<?php echo "?u=",$username;?>">Go to Profile</a>
    </div>
    </form>
    </div>
    <?php include_once("template_pageBottom.php"); ?>
    </body>
</html>

Upvotes: 0

Views: 118

Answers (1)

Rikesh
Rikesh

Reputation: 26441

Just a guess you comparing username field with firstname,

SELECT * FROM users WHERE firstname='" . $username . "'";

While it needs to be,

SELECT * FROM users WHERE username='" . $username . "'";

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Upvotes: 3

Related Questions