HTML表单:选择选项以将数据插入MySQL phpmyadmin数据库

我有一个名为“ add_report”的php文件,里面有一个表单.我所有的输入都在运行,我可以将数据输入到数据库中,但是每次使用select-option时.我的数据库将其接受为null.这是为什么?这是我的表格“ add_report.php”di...

我有一个名为“ add_report”的php文件,里面有一个表单.我所有的输入都在运行,我可以将数据输入到数据库中,但是每次使用select-option时.我的数据库将其接受为null.这是为什么?

这是我的表格“ add_report.php”

<div class="wrapper">
    <form action="add_report_backend.php" method="post">
      <input type="hidden" name="id">
      <label>Agency: </label> <input class="input1" type="text" name="agency" value="CAAP" required readonly><br>
      <label>File Name: </label> <input class="input2" type="text" name="filename" placeholder="file.pdf/xlsx/xls/docx" required autofocus><br>
      <label>File Type:  &nbsp;</label> <select name="myselectbox">
        <option name="myoption1" value="myoption1">pdf</option>
        <option name="myoption2" value="myoption2">excel</option>
        <option name="myoption3" value="myoption3">word</option>
      </select><br>
      <label>Date: </label> <input class="input4" type="Date" name="date" required><br>
  <input class="submit-btn" type="submit" name="insert" value="Save">
</form>
</div>

而这另一个PHP文件“ add_report_backend.php”

<?php
if(isset($_POST['insert']))
{
    try {
    $pdoConnect = new PDO("mysql:host=localhost;dbname=annualdb","root","");

    $pdoConnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }
    $id = $_POST['id'];
    $Agency = $_POST['agency'];
    $FName = $_POST['filename'];
    $FType = $_POST['filetype'];
    $Date = $_POST['date'];

    $pdoQuery = "INSERT INTO `company_report`(`agency`, `filename`, `filetype`, `date`) VALUES (:Agency,:FName,:FType,:Date)";
    $pdoResult = $pdoConnect->prepare($pdoQuery);
    $pdoExec = $pdoResult->execute(array(":Agency"=>$Agency,":FName"=>$FName,":FType"=>$FType, ":Date"=>$Date));

    if($pdoExec)
    {

        $pdoQuery = 'SELECT * FROM company_report';
        $pdoResult =  $pdoConnect->prepare($pdoQuery);
        $pdoResult->execute();
            while ($row = $pdoResult->fetch()){
                echo  $row['id'] . " | " .$row['agency'] . " | " . $row['filename'] . " | " . $row['filetype'] . " | " . $row['date'];
            }
            header("Location: ../agencies/company.php");
            exit;
        } else {
            echo 'Data Not Inserted';
    }
}
$pdoConnect = null;
?>

解决方法:

HTML名称属性和$_POST名称应该相同.

你需要改变

$FType = $_POST['filetype'];

通过

$FType = $_POST['myselectbox'];

本文标题为:HTML表单:选择选项以将数据插入MySQL phpmyadmin数据库

基础教程推荐