0

My code is already inserting a data on the database, but only the Primary key(AUTO_INCREMENT) is the only adding. I can't get the date and the text.

Is there something wrong in my code? Here is my code below:

HTML:

<form action="insertleave.php" method="post">
     <label>Date Filed:</label>
      <input type="date" name="datefiled">

      <label>Date of Leave:</label>
      <input type="date" name="leavedate">

  </div>
  <div class="medium-6 columns">
    <label>Reason of Leave:</label>
    <textarea rows="8" form="leaveform" name="reason"></textarea>
  </div>
  <input type="submit" class="expanded button" name="formSubmit" value="File Leave">
  </form>

PHP:

<?php
$datefiled = $_POST['datefiled'];
$leavedate = $_POST['leavedate'];
$leavereason = $_POST['leavereason'];

$config = parse_ini_file("phpconfig.ini");
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['dbname']);


if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


   $sql = "INSERT INTO leaves (ID, EmployeeID,DateFiled, LeaveDate, Reason)
   VALUES
   ('$ID','$EmployeeID','$DateFiled','$LeaveDate','$Reason')";


  if (mysqli_query($conn, $sql)) {
    echo "OK!";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
0

2 Answers 2

1

In your text area, you given it a name "reason"

in your post variable, your value is "leavereason"

change the $leavereason = $_POST['leavereason']; to $leavereason = $_POST['reason'];

Sign up to request clarification or add additional context in comments.

Comments

0

In you Reason of leave text area name of input is different.

Your variable name are different in your sql query and you are assigning to different variable.

Also your EmployeeID is empty here. there is no input for EmployeeID from html file or you should post it to php file.

Change you php code like this.

<?php
$datefiled = $_POST['datefiled'];
$leavedate = $_POST['leavedate'];
$leavereason = $_POST['reason'];

$config = parse_ini_file("phpconfig.ini");
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['dbname']);


if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


   $sql = "INSERT INTO leaves (ID, EmployeeID,DateFiled, LeaveDate, Reason)
   VALUES
   ('$ID','$EmployeeID','$datefiled','$leavedate','$leavereason')";


  if (mysqli_query($conn, $sql)) {
    echo "OK!";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.