SweetAlert在尝试使用php将数据插入MySql数据库后通知是否成功或失败

问题描述

我正在使用这些html,php和ajax在mysql数据库中插入数据。我想使用SweetAlert在ajax成功函数中显示成功或错误消息。数据不会插入数据库,但是即使单击“提交”按钮也能显示消息,我的代码如下;

server.php

<?php
session_start();

// initializing variables
$username = "";
$motor_number    = "";
$phone_number    = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost','root','','registration');

// REGISTER USER
if (isset($_POST['reg_user'])) {
      // receive all input values from the form
      $username = mysqli_real_escape_string($db,$_POST['username']);
      $phone_number = mysqli_real_escape_string($db,$_POST['phone_number']);
      $locationn = mysqli_real_escape_string($db,$_POST['locationn']);
      $region = mysqli_real_escape_string($db,$_POST['region']);
      $motor_number = mysqli_real_escape_string($db,$_POST['motor_number']);
      $password_1 = mysqli_real_escape_string($db,$_POST['password_1']);
      $password_2 = mysqli_real_escape_string($db,$_POST['password_2']);

    // form validation: ensure that the form is correctly filled ...
    // by adding (array_push()) corresponding error unto $errors array
    if (empty($username)) { array_push($errors,"Username is required"); }
    if (empty($phone_number)) { array_push($errors,"Phone Number is required"); }
    if (empty($locationn)) { array_push($errors,"Location is required"); }
    if (empty($region)) { array_push($errors,"Region  is required"); }
    if (empty($motor_number)) { array_push($errors,"Motor number is required"); }
    if (empty($password_1)) { array_push($errors,"Password is required"); }
    if ($password_1 != $password_2) {array_push($errors,"The two passwords do not match");}

    // first check the database to make sure 
    // a user does not already exist with the same username and/or email
    $user_check_query = "SELECT * FROM user WHERE username='$username' OR motor_num='$motor_number' LIMIT 1";
    $result = mysqli_query($db,$user_check_query);
    $user = mysqli_fetch_assoc($result);

    if ($user) { // if user exists
      if ($user['username'] === $username) {
           array_push($errors,"Username already exists");
      }

      if ($user['motor_num'] === $motor_number) {
          array_push($errors,"Motor Number already exists");
      }
    }

    // Finally,register user if there are no errors in the form
    if (count($errors) == 0) {
        $password = md5($password_1);//encrypt the password before saving in the database

        $query = "INSERT INTO user (username,phone_num,location,region,motor_num,password) 
              VALUES('$username','$phone_number','$locationn','$region','$motor_number','$password')";
      mysqli_query($db,$query);

    }
  }


  

这是jQuery代码

     $(function(){
    $('#reguser').click(function(e){

        var valid = this.form.checkValidity();

        if(valid){


                var username    =   $('#username').val();
                var phone_number    =   $('#phone_number').val();
                var locationn   =   $('#locationn').val();
                var region      =   $('#region').val();
                var motor_number    =   $('#motor_number').val();
                var password_1  =   $('#password_1').val();
                var comfirm_password    = $('#comfirm_password').val();
    
                    e.preventDefault(); 

                    if(username == '' || phone_number == '' || locationn == '' || region == '' || motor_number == ''){
                swal("Oops!!","Looks like you missed some fields. Please check and try again!","error");}
                else{
                    $.ajax({
                    type:'post',url:'server.php',data: {username:username,phone_number:phone_number,locationn:locationn,region:region,motor_number:motor_number,password_1:password_1,comfirm_password:comfirm_password},success:function(data){
                        swal("Success","Data Saved Successfully","successs");
                    },error:function(xhr,thrownError,ajaxOptions){

            }
   });

}

                        
        }

        

    });     

}); ```

如果有人能帮助我解决问题,我将不胜感激。在此先感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...