-1

I am stuck with a problem , I am getting an "failed" result while submitting the signup form, I don't know where I am wrong, please help.

ajaxrequest.js

 function addStu() {
    var stuname = $("#stuname").val();
    var stuemail = $("#stuemail").val();
    var stupass = $("#stupass").val();

    $.ajax({
        url:'Student/addStudent.php',
        method:"POST",
        dataType: "json",
        data:{
            stuname : stuname,
            stuemail : stuemail,
            stupass : stupass,
        },
        success:function(data){
            console.log(data);
            if(data == "OK"){
                $('#successMsg').html("<span>Registration Successfull!</span>")
            }
            else if(data === "failed"){
                $('#successMsg').html("<span> Unable to Register!</span>")

            }
        },
    });
}

addStudent.php

This is the addstudent.php form where wrote i insert data code

<?php
   include_once('../dbconnection.php');
   if(isset($_POST['stuname']) && isset($_POST['stuemail']) && isset($_POST['stupass']))

   $stuname = $_POST['stuname'];
   $stuemail = $_POST['stuemail'];
   $stupass = $_POST['stupass'];

   $sql = "INSERT INTO student(stu_name, stu_email, stu_pass) VALUES('$stuname','$stuemail','$stupass')";

  if( $conn->query($sql)== TRUE){
    echo json_encode("OK");
  }
  else{
      echo json_encode("failed");
  }
   
?>
  • 1
    You're missing a quote after `'$stupass`. Have a look at how to [output MySQL errors](https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query). – KIKO Software Feb 26 '21 at 11:22
  • And please consider switching to prepared, parametrized queries to [prevent SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – El_Vanja Feb 26 '21 at 12:01

1 Answers1

0

Try using prepared parametrized statements and check if it executes.

  <?php
    include_once('../dbconnection.php');
    if(isset($_POST['stuname']) && isset($_POST['stuemail']) && isset($_POST['stupass']))
    
        $stuname = $_POST['stuname'];
        $stuemail = $_POST['stuemail'];
        $stupass = $_POST['stupass'];
    
        $sql = "INSERT INTO student (stu_name, stu_email, stu_pass) VALUES (:stuName, :stuEmail, :stuPass)";
        $sql = $conn->prepare($sql);
        $sql->bindParam(":stuName",$stuname);
        $sql->bindParam(":stuEmail",$stuemail);
        $sql->bindParam(":stuPass",$stupass);
    
        if($sql->execute()){
            echo json_encode("OK");
        }else{
            echo json_encode("failed");
        }
    ?>
Searle
  • 54
  • 9