Jump to content

Prevent Blank entry into database and doesnt allow duplicate entry in database


sjthetechguy
Go to solution Solved by kicken,

Recommended Posts

Hi, I have this code, which is working ok to add an entry into the database.

Need help with the following:
1.it also adds a empty record when i submit the form blank - whoich I dont want
2. I have 3 columns in table, id, offices and country. - I want the entry to be made into database if the offices doesnt already exist into the databse.

 

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

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

 $nameoffice = $_POST["officeinput"];


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


}

?>



<div class="page-body">

<br><br>



               <div class="card">
                  <div class="card-header">
                    <h5>Add Office</h5>
                  </div>
                  <form class="form theme-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>


              <!-- Zero Configuration  Starts-->
              <div class="col-sm-12">
                <div class="card">
                  <div class="card-header pb-0 card-no-border">
                    <h3>Zero Configuration</h3><span>DataTables has most features enabled by default, so all you need to do to use it with your own tables is to call the construction function:<code>$().DataTable();</code>.</span><span>Searching, ordering and paging goodness will be immediately added to the table, as shown in this example.</span>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="display" id="basic-1">
                        <thead>
                          <tr>
                            <th>Offices</th>
                            <th>Country</th>
                          </tr>
                        </thead>


                        <tbody>
                          <tr>
                            <td>Tiger Nixon</td>
                            <td>System Architect</td>
                            <td>Edinburgh</td>
                            <td>61</td>
                            <td>2011/04/25</td>
                            <td>$320,800</td>
                            <td> 
                              <ul class="action"> 
                                <li class="edit"> <a href="#"><i class="icon-pencil-alt"></i></a></li>
                                <li class="delete"><a href="#"><i class="icon-trash"></i></a></li>
                              </ul>
                            </td>
                          </tr>
 
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
           
              <!-- Zero Configuration  Ends-->

<?php

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

?>

 

Link to comment
Share on other sites

  • Solution
19 minutes ago, sjthetechguy said:

it also adds a empty record when i submit the form blank - whoich I dont want

Then you need to validate the entered value to ensure it is not empty before running your query.

19 minutes ago, sjthetechguy said:

I want the entry to be made into database if the offices doesnt already exist

Then you need to add a unique constraint to your database table to prevent duplicates.

Edited by kicken
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.