Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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;
?> 
share|improve this question
up vote 1 down vote accepted

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"];
share|improve this answer
    
I pass the values through Ajax but I don't know if I do it correctly. See data in my ajax call. – GeorgeGkas Dec 12 '15 at 12:08
    
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. – Chris Banks Dec 12 '15 at 13:01
    
@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? – GeorgeGkas Dec 12 '15 at 16:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.