skip to Main Content

I have a php file named “add_report” with a form inside it. All my inputs are running, i can input data into my database, but everytime I use the select-option. my database accepts it as null. Why is that?

This is my form “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>

And this another php file “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;
?>

2

Answers


  1. Change $FType = $_POST['filetype']; to $FType = $_POST['myselectbox'];

    Login or Signup to reply.
  2. The HTML name attribute and the $_POST name should be the same.

    You need to change

    $FType = $_POST['filetype'];
    

    by

    $FType = $_POST['myselectbox'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search