1

I recently create a web project. I want to update a mysql query through ajax and PHP. I created the code but I had problems. My server uses PHP 5.2*. When I press the button to update the database sometimes nothing happens and sometimes I get database error. I don't know exactly where is the problem cause it's the first time I working in back-end dev so any help is appreciated!

    --------------------HOME.JS--------------------
 $('#update_profile').click(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'lib/preferences.php',
            data: {
                email: $('#pref-changes input[name="email"]').val(),
                pass: $('#pref-changes input[name="pass"]').val(),
                username, $('#pref-changes input[name="username"]').val(),

            },
            dataType: "html",
            success: function(data){
                    window.location.href = data;
                }
        });
});


--------------------HOME.HTML--------------------
<form class="pref-changes" id="pref-changes">
        <div class="pref_avatar">
           <div class="avatar_change">Change</div>
        </div> 

        <div style="margin-top: 10px;">
           <label>Change Username</label>
           <input name="username" class="pref_inp" placeholder="GeorgeGkas" type="text">
        </div>

        <div class="lbl">
           <label>Change Email</label>
           <input name="email" class="pref_inp" placeholder="[email protected]" type="email">
        </div>

        <div class="lbl">
           <label>Change Password</label>
           <input name="pass" class="pref_inp" placeholder="Password" type="password">
        </div>

        <div class="update_btn">
          <button type="submit" id="update_profile" name="update_profile">Update</button>
        </div>

</form>


 --------------------PREFERENCES.PHP--------------------
<?php
  session_start();

    define("DB_HOST", 'mysql6.000webhost.com');
    define("DB_USER", '');
    define("DB_PASSWORD", '');
    define("DB_DATABSE", '');
    $UserEmail = $_SESSION['login'];
    $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_DATABSE, $conn);

    if ($email != "") {
      $sql = "UPDATE Users SET UserEmail='".$email."' WHERE UserEmail=".$UserEmail."";
      mysql_query($conn, $sql);
    }

    if ($pass != "") {
      $sql = "UPDATE Users SET UserPass='".$pass."' WHERE UserEmail=".$UserEmail."";
      mysql_query($conn, $sql);
    }

    if ($username != "") {
      $sql = "UPDATE Users SET UserName='".$username."' WHERE UserEmail=".$UserEmail."";
      mysql_query($conn, $sql);
    }

    $host  = $_SERVER['HTTP_HOST'];
    $link = "http://$host/home.php";
    echo $link;
?> 

1 Answer 1

1

On your PHP code do you have defined $email, $pass or $username ? Perhaps you need this before you check if they are diferente from ""

$email = $_POST["email"];
$pass = $_POST["pass"];
$username = $_POST["username"];
Sign up to request clarification or add additional context in comments.

3 Comments

I pass the values through Ajax but I don't know if I do it correctly. See data in my ajax call.
POST values are referenced inside of the $_POST variable. Change your code to use what Ricardo suggested. It would also help if you posted the database error message. Also... your code is also not secure at all, use mysql_real_escape_string to make your database safe.
@Clayton this is just a project. The secure part will be handle lately. As for the PHP I didn't know that I have to pass the values through POST. Is there any other part of my code that need fix?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.