Nyawk Nyawk
Nyawk Nyawk

Reputation: 45

I want to insert data like text and date from HTML FORM to PHP to MYSQL Database

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);
?>

Upvotes: 0

Views: 327

Answers (2)

Rahul
Rahul

Reputation: 726

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);
?>

Upvotes: 0

NewbieDeveloper
NewbieDeveloper

Reputation: 126

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'];

Upvotes: 1

Related Questions