简体   繁体   English

jQuery POST仅向mysql添加记录一次

[英]JQuery POST only adding record to mysql once

I'm using JQuery to call a php script which will add a record to the mySQL database. 我正在使用JQuery调用php脚本,它将向mySQL数据库添加一条记录。

This works fine for the first time I try to add something, but if I want to add more records to the database, it will not add anything. 第一次尝试添加内容时,此方法效果很好,但是如果我想向数据库添加更多记录,它将不会添加任何内容。 There are no errors in the console or php error log. 控制台或php错误日志中没有错误。 It does not work if I refresh or reopen the page either. 如果我刷新或重新打开页面也无法正常工作。

jquery: jQuery的:

$(document).ready(function(){
    $("#myForm").submit(function(){

        var username = $("#username").val();
        var password = $("#password").val();
        var repassword = $("#repassword").val();
        var email = $("#email").val();
        var reemail = $("#reemail").val();

        if(password != repassword)
        {
            alert("Passwords do not match");
            return false; 

        }

        else if(email != reemail)
        {
            alert("Emails do not match");
            return false; 

        }

        else {

            $.post("signup_script.php",
            {
                username: username,
                password: username,
                email: email
            });
        }
    });
});

php: PHP:

<?php 

    include_once('profile_Functions.php');

    $username = $_POST['username']; 
    $password = $_POST['password'];
    $email = $_POST['email'];

    createUser($username,$password,$email);

?>

html: HTML:

<div class="body"></div>
    <div class="grad"></div>
    <div class="header">
        <div>odd<span>job</span></div>
    </div>
    <br>
    <div class="signup">

<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script type = "text/javascript" src="js/js_functions.js"></script>

<form  id ="myForm" action =""  method = "post" >
            <input   type="text" placeholder="username" id ="username" required = "required" maxlength = "15"><br>
            <input type ="email" placeholder = "email" id = "email" required = "required" maxlength = "50"><br>
            <input type ="email" placeholder = "re-enter email" id = "reemail" required = "required" maxlength = "50"><br>
            <input  type="password" placeholder="password" id="password" required = "required" pattern = "(?=.*\d)(?=.*[A-Z]).{10,}"><br>
            <input  type="password" placeholder="re-enter password" id ="repassword" required = "required"><br>
            <p class = "passwordreq">Password must:</p>
            <ol class = "passwordreq"> 
                <li>Have 10 characters</li>
                <li>Have one number</li>
                <li>Have one uppercase letter</li>
            </ol>
            <input type="submit" value="sign up" id ="submit"> 
            <input type="button" value="go back"  onclick="window.location='index.html'">
        </form>

insert function: 插入功能:

function setup(){
extract($_GET);
extract($_POST);
extract($_SERVER);

$host="localhost";
$user="root";
$passwd="";

$connect=mysql_connect($host,$user,$passwd) or die("error connecting mysql server");
mysql_select_db('oddjob',$connect) or die("error accessing db");
}

function createUser($name, $pass, $email){
setup();
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$sql = "insert into profiles (UserName, Password, Email) values ('$name', '$hashed_password', '$email'); ";
$res = mysql_query($sql);

}

您应该每次都关闭连接

mysql_close($connect)

I wonder if the solution is related to this question: https://stackoverflow.com/a/6143475/652519 . 我想知道解决方案是否与此问题有关: https : //stackoverflow.com/a/6143475/652519 Can you add the error function to your post function? 您可以将error函数添加到post函数中吗? Like so: 像这样:

$.post("signup_script.php",
        {
            username: username,
            password: username,
            email: email
        })
        .error(function() { alert("error"); });

Then we can see if there are errors that happen upon insert in the DB. 然后,我们可以查看在DB中插入时是否发生错误。

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

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