Click here to Skip to main content
14,606,837 members

Data does not get inserted into DB with PHP

Member 14790760 asked:

Open original thread
Basically what happens is anytime I try to submit the form nothing gets posted into my DB. I'm not sure what is wrong with my code.
<?php
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  ini_set("error_log",dirname(__FILE__).'/errorlog.txt');
  session_start();

  if(isset($_POST['submit'])){
    $conn = new mysqli('localhost','root','','employeeTS');

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


    $hours= $_POST['hourPHP'];
    $rate= $_POST['ratePHP'];
    $date= $_POST['datePHP'];
    $user= $_SESSION['email'];
    //prepared statement
    $stmt =$conn->prepare("INSERT INTO Paysheet(`Hours`,`Rate`,`Date`,`Employee_email`) VALUES(?,?,?,?)");
    $stmt->bind_param("idss",$hours,$rate,$date,$user);
    $stmt->execute();

    echo "New record created succesfully";

    $stmt->close();
    $conn->close();
  }
?>


And here's my HTML with jQuery/AJAX:
<html>
  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
     integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>

    <nav class="navbar navbar-expand-sm bg-dark navbar-dark justify-content-center">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="">Income Calculator</a>
          <a class="nav-link justify-content-right" href="logout.php">Logout</a>
      </ul>
    </nav>
    <div class="jumbotron justify-content-center">
      <h1 class="text-info justify-content-center">Get Your Income (Tax Feature Coming Soon)!</h1>
    </div>
    <div class="container">
      <div  class="row">
        <div class="col"></div>
        <div class="col-4 justify-content-center">
          <div class="card bg-light">
            <div class="card-header">Input Your Rate and Hours</div>
            <div class="card-body">
              <form method="post" action="hourspage.php">
                <div class="form-group row">
                  <label class="col-sm-4 col-form-label"for="hours">Hours</label>
                  <div class ="col-sm-5">
                    <input type="number" min="1" max="24"class="form-control"id="hours" class="form-control " placeholder="Enter Hours"required>
                  </div>

                </div>
                <div class="form-group row">
                  <label class="col-sm-4 col-form-label"for="rate">Rate</label>
                  <div class="col-sm-5">
                    <input type="number" step=".01"class="form-control" id="rate"  placeholder="Enter Rate" required >
                  </div>
                </div>
                <div class="form-group row">
                  <label class="col-sm-4 col-form-label"for="date">Date</label>
                  <div class="col-sm-5">
                    <input type="date"class="form-control" id="date" required >
                  </div>
                </div>
                <div class="form-group row">
                  <div class="dropdown">
                    <div class="col-sm-4">
                      <select class="form-control-4">
                        <option value="Wisconsin">Wisconsin</option>
                        <option value="Illinois">Illinois</option>
                      </select>
                    </div>
                  </div>

                </div>
                <div class="form-group row">
                  <div class="col-sm-6">
                    <button type="submit" class="btn btn-primary" id="submit">Submit Hours</button>
                  </div>
                </div>
              </form>
              <p class="income_field">Your income is $<span id="incomef"></span>.</p>
              <p id="response"></p>
              </div>
            </div>

        </div>
      <script
      src="https://code.jquery.com/jquery-3.4.1.min.js"
      integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
      crossorigin="anonymous"></script>
      <script type="text/javascript">
        $(document).ready(function(){
          //function to determine total pay-out
          $('#submit_pay').on('click',function(){
            var hoursf=$("#hours").val();
            var ratef=$("#rate").val();
            var datef=$("#date").val();
            var statef = $('#dropdown-item').val();
            //validation check to see if all fields have been filled
            if(hoursf==""&&ratef==""){
              alert("Please fill out all fields");
            }else{
            if(hoursf==""||ratef==""){
                alert("Please fill out required field");
              }}




            //send data off to server
            $.ajax(
              {
                url:"hourspage.php",
                type:"POST",
                data:{
                  hourPHP:hoursf,
                  ratePHP:ratef,
                  datePHP:datef
                },
                success: function(response){

                  $('#response').html(response);
                  console.log(data);

                    },
                datatype:"text"
              }
            );
            //calc income so user knows how much they are getting
            var incomef = hoursf*ratef;
            $('span#incomef').text(incomef);
            $('p#income_field').show();


          })
        });
      </script>
    <div class="col"></div>
    </div>
  </div>
  </body>
</html>


What I have tried:

I thought it might have something to do with the AJAX request not sending thus affecting the ability of PHP's $_POST to in the condition to fail but I don't necessarily understand what exactly is wrong with the response. Please advise.
Tags: HTML, PHP, Ajax

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100