Reputation:
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;
?>
Upvotes: 1
Views: 807
Reputation: 529
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"];
Upvotes: 1