Update MySQL database with PHP and AJAX

user4108694

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;
?> 
Ricardo Seromenho

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"];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql UPDATE using PHP and AJAX, can't update database

From Dev

jQuery ajax post to php script & update mysql database

From Dev

Update database with html link click using ajax php mysql

From Dev

PHP and AJAX update data to database

From Dev

MYSQL and PHP database update wont update database

From Dev

PHP MySql Database Array update

From Dev

Insert and update database with php mysql

From Dev

Update database using MySQL and PHP

From Dev

Add to database with mysql, ajax and php

From Dev

AJAX update MYSQL database using function called from HTML generated from PHP

From Dev

AJAX update MYSQL database using function called from HTML generated from PHP

From Dev

Update mysql table with php using ajax

From Dev

Update form using Ajax, PHP, MYSQL

From Dev

update mysql with ajax and php (using jquery)

From Dev

PHP / MySQL / AJAX - Update multiple data

From Dev

Update value in mysql database column with checkbox in php

From Dev

Mysql and PHP - Update database from a different server

From Dev

php mysql update database on button click

From Dev

Update value in mysql database column with checkbox in php

From Dev

Database - mySQL and PHP updating conflicting information on update

From Dev

Update MySQL database using php statement

From Dev

PHP/MySQL Update database on session timeout

From Dev

Get user id for database update - PHP/MySQL

From Dev

Trying to update mysql database form in php

From Dev

PHP & MySql: can't update database

From Dev

cannot update quantity in stock on database php/mysql

From Dev

Insert records to mysql database with php using Ajax

From Dev

Searching MySQL database with PHP, AJAX and jQuery

From Dev

Searching MySQL database with PHP, AJAX and jQuery

Related Related

  1. 1

    Mysql UPDATE using PHP and AJAX, can't update database

  2. 2

    jQuery ajax post to php script & update mysql database

  3. 3

    Update database with html link click using ajax php mysql

  4. 4

    PHP and AJAX update data to database

  5. 5

    MYSQL and PHP database update wont update database

  6. 6

    PHP MySql Database Array update

  7. 7

    Insert and update database with php mysql

  8. 8

    Update database using MySQL and PHP

  9. 9

    Add to database with mysql, ajax and php

  10. 10

    AJAX update MYSQL database using function called from HTML generated from PHP

  11. 11

    AJAX update MYSQL database using function called from HTML generated from PHP

  12. 12

    Update mysql table with php using ajax

  13. 13

    Update form using Ajax, PHP, MYSQL

  14. 14

    update mysql with ajax and php (using jquery)

  15. 15

    PHP / MySQL / AJAX - Update multiple data

  16. 16

    Update value in mysql database column with checkbox in php

  17. 17

    Mysql and PHP - Update database from a different server

  18. 18

    php mysql update database on button click

  19. 19

    Update value in mysql database column with checkbox in php

  20. 20

    Database - mySQL and PHP updating conflicting information on update

  21. 21

    Update MySQL database using php statement

  22. 22

    PHP/MySQL Update database on session timeout

  23. 23

    Get user id for database update - PHP/MySQL

  24. 24

    Trying to update mysql database form in php

  25. 25

    PHP & MySql: can't update database

  26. 26

    cannot update quantity in stock on database php/mysql

  27. 27

    Insert records to mysql database with php using Ajax

  28. 28

    Searching MySQL database with PHP, AJAX and jQuery

  29. 29

    Searching MySQL database with PHP, AJAX and jQuery

HotTag

Archive