我想将文本和日期等数据从HTML FORM插入PHP到MYSQL数据库

时间:2016-03-28 03:41:17

标签: php html mysql date insert

我的代码已在数据库中插入数据,但只有主键(AUTO_INCREMENT)才是唯一的添加。我无法得到日期和文字。

我的代码中有什么问题吗? 以下是我的代码:

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

2 个答案:

答案 0 :(得分:1)

在你的文字区域,你给它起了一个名字&#34;原因&#34;

在您的帖子变量中,您的值为&#34; leavereason&#34;

$leavereason = $_POST['leavereason'];更改为$leavereason = $_POST['reason'];

答案 1 :(得分:0)

在您输入的原因文本区域name不同的原因。

您的sql查询中的变量名称不同,并且您要分配给不同的变量。

此处EmployeeID也是空的。从html文件中没有EmployeeID的输入,或者你应该将它发布到php文件。

像这样改变你的PHP代码。

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