Jump to content

PHP form not posting on SQL database


sjthetechguy
Go to solution Solved by kicken,

Recommended Posts

Hi, I have this code written, when I input and submit the form, it doesnt post on the databse.

Please note that the functions.php file already includes the database connection

<?php
include("./functions.php");
include("../includes/header_internal.php");
include("../includes/sidebar_internal.php");

?>



<div class="page-body">

<br><br>

               <div class="card">
                  <div class="card-header">
                    <h5>Add Office</h5>
                  </div>
                  <form class="form theme-form">
                  <form method="post" action="offices.php">
                    <div class="card-body">
                      <div class="row">
                        <div class="col">
                          <div class="mb-3 row">
                            <label class="col-sm-3 col-form-label">Office Name</label>
                            <div class="col-sm-9">
                              <input class="form-control" type="text" name="officeinput">
                            </div>
                          </div>

                    <div class="card-footer text-end">
                      <div class="col-sm-9 offset-sm-3">
                        <button class="btn btn-primary" type="submit">Add Office</button>
                        <input class="btn btn-light" type="reset" value="Cancel">
                      </div>
                    </div>
                  </form>
                </div>

</div>

<?php

if (isset($_POST['submit'])) {

  include("../externalconnectors/database_variables/database_and_databse_tables.php");
  $nameoffice = $_POST["officeunput"];


  // Insert the data into the database
  $queryoffice = "INSERT INTO firmoffices (offices) VALUES ('$nameoffice')";
  mysqli_query($conn, $queryoffice);

}


?>

<?php

include("../includes/scripts.php");
include("../includes/footer_internal.php");

?>

 

Edited by sjthetechguy
Link to comment
Share on other sites

  • Solution
12 minutes ago, sjthetechguy said:
                  <form class="form theme-form">
                  <form method="post" action="offices.php">

You cannot nest forms.  That should just be a single form tag with all the attributes, not separate.

13 minutes ago, sjthetechguy said:
if (isset($_POST['submit'])) {

This will only be true if you have an input in your form with name="submit".  Since you do not, it will never be true.  A generic test for a form post is to test the request method.

if ($_SERVER['REQUEST_METHOD']==='POST'){

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.