4

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;
?>
3
  • Have you checked if posted datas are not null before it saves to your database? Commented Mar 7, 2019 at 9:10
  • The data in my table are NULL as default, should I change them as not null? Commented Mar 7, 2019 at 9:31
  • What have you tried to debug the problem? Have you checked whether the proper data is transmitted to the server, whether it reaches the server, whether the proper variables where filled? Commented Mar 7, 2019 at 9:44

2 Answers 2

2

The HTML name attribute and the $_POST name should be the same.

You need to change

$FType = $_POST['filetype'];

by

$FType = $_POST['myselectbox'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Pupil! It worked I also changed the value of my options to value="pdf", "excel" and "word" too.
2

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

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.