简体   繁体   English

使用 jquery ajax 将数据发布到数据库时遇到问题

[英]Trouble with posting data to database using jquery ajax

I have looked up tutorials multiple times and am having trouble figuring out where my code is wrong.我已经多次查找教程,但无法找出我的代码错误的地方。 I am able to run my php file and get my information sent into my database, so I know my connection isn't the issue, so I am thinking its my index.html file.我能够运行我的 php 文件并将我的信息发送到我的数据库中,所以我知道我的连接不是问题,所以我认为它是我的 index.html 文件。 It may have something to do with how i tried to implement jquery, but I don't know about that either这可能与我尝试实现 jquery 的方式有关,但我也不知道

Index.html索引.html

 <!DOCTYPE html>
<html lang="en">
<head>
  
  <link rel="stylesheet" href="login.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    
    <script>
    $(document).ready(function(){
      $('#login').click(function(event){
        event.preventDefault();
          var username =$('#use').val();
          var pass = $('#pass').val();
  
          $.ajax({
              url: 'login.php',
              method:'POST',
              data:
              {
                  User: username,
                  Pass: pass
              },
              success:function(result){
                  alert(result);
              }
          });
      });
  });</script> 
</head>
<body>
    <div class="login-page">
        <div class="form">
          <form class="register-form">
            <input type="text" placeholder="name"/>
            <input type="password" placeholder="password"/>
            <input type="text" placeholder="email address"/>
            <button>create</button>
            <p class="message">Already registered? <a href="#">Sign In</a></p>
          </form>
          <form class="login-form">
            <input type="text" id="use" placeholder="username"/>
            <input type="password" id="pass" placeholder="password"/>
            <button type="submit" id="login">login</button>
            <p class="message">Not registered? <a href="#">Create an account</a></p>
          </form>
        </div>
      </div>
</body>
</html>

PHP code connecting to my DB PHP 代码连接到我的数据库

<?php
$dbname = 'project test';
$dbuser = 'root';
$dbpass = '';
$dbhost = 'localhost';

$username=$_POST['use'];
$password=$_POST['pass'];

$conn= mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  } 

$sql = "INSERT INTO user (User, Pass)
    VALUES ('{$username}','{$password}')";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close(); ;

?>

In your index.html file, you are sending User and Password values However, when you are trying to get them in your php file, you are trying to get use and pass values.在您的 index.html 文件中,您发送的是UserPassword值。但是,当您尝试在 php 文件中获取它们时,您正在尝试获取usepass值。 Their names don't match.他们的名字不匹配。 The names need to match in order to get the values correctly.名称需要匹配才能正确获取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM