dqst96444 2015-12-12 11:56
浏览 30
已采纳

使用PHP和AJAX更新MySQL数据库

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="georgegkas@gmail.com" 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条回答 默认 最新

  • dongtuo6562 2015-12-12 12:03
    关注

    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"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题