Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having an issue with duplicate data being inserted into my database table called 'family'. Here's the relevant code:

HTML Form:


  <form action="{{url('requestFormData')}}" id="multi-step-form" class="multi-step-form" method="POST">
                                    <div>
                                           
                                             @csrf
                                         <section class ="form-section">
                                         <h4>Client Info</h4>
                                            <div class="row">
                                                <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">First Name*</label>
                                                        <input type="text" name="firstName" class="form-control" placeholder="FirstName" required>
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Last Name*</label>
                                                        <input type="text" name="lastName" class="form-control" placeholder="LastName" required>
                                                    </div>
                                                </div>
                                              
                            
                                                  <div class="card-body col-lg-2 mb-4">
                                                    <div class="form-group">
                                                  <p class="mb-0">Referal date</p>
                                                   <input name="datepicker" class="datepicker-default form-control" id="datepicker">
                                                  </div>
                                                   </div>

                                                 
                                                   <div class="card-body col-lg-2 mb-4">
                                                    <div class="form-group">
                                                  <p class="mb-0">Contact date</p>
                                                   <input name="datepicker" class="datepicker-default form-control" id="datepicker">
                                                  </div>
                                                   </div>
                                               
                                                
                                                 
                                                  
                                                   <div class="col-lg-5 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Phone Number*</label>
                                                        <input type="text" name="phone_number" class="form-control" placeholder="Phone Number" required>
                                                    </div>
                                                </div>


                                                <div class="col-lg-5 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Move in date*</label>
                                                        <input type="text" name="moveindate" class="form-control" placeholder="Move In Date" required>
                                                    </div>
                                                </div>




                                                <div class="col-lg-12 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label"> Address</label>
                                                        <div class="input-group">
                                                            <input type="text" class="form-control"  name="address" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="address 1" required>
                                                        </div>
                                                    </div>
                                                </div>
                                               

                                                <div class="col-lg-1 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Number of Adults *</label>
                                                        <input type="number" name="numberofAdults" id = "numberofAdults" min ="0"  max ="10" class="form-control" onchange="addAdultFields()" required>
                                                    </div>
                                                </div>

                                                <div class="col-lg-1 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Number of Children*</label>
                                                        <input type="number" name="numberofChildren" id = "numberofChildren" min ="0" max ="10" class="form-control" onchange="addChildrenFields()" >
                                                    </div>
                                                </div>

                            
                                               
                                      </section>

  
                                <button type="button" class ="  previous btn btn-primary float-left"> previous </button>
                                
                                <button type="button" class =" next btn btn-primary float-right">Next  </button>

                                <button type="submit" class =" btn btn-success float-right"> Submit </button>

                                </div>
here is my php code which submits to the database

  public function postdata(Request $req){


   // handeling first serction of multi step form 
         $firstname = $req ->input('firstName'); // taking data from the firstname text field on the request form 
         $lastname  = $req ->input('lastName'); // taking data form the lastname text field on the request form
         $phone_num = $req ->input('phone_number'); // ||
         $address   = $req ->input('address'); // ||
         $numAdults = $req ->input('numberofAdults'); //||
         $numkids   = $req ->input('numberofChildren');
        $data = array('headofhousehold_firstname' => $firstname, "headofhousehold_lastname" => $lastname, "phone" => $phone_num, "address" => $address, "numofadults" => $numAdults, "numofkids" => $numkids);
       

        
        DB::table('family') -> insertOrIgnore($data);
       
 
here is my route

 Route::get('postdata','RequestInsertController@insform'); // this route is used to display the form view when /postdata is accessed 

   
Route::post('requestFormData',[RequestInsertController::class, 'postdata'])->name('postdata');
when I click the submit it work but i get a duplicate with each user but they have a different family id. example I add a user John Doe I get it twice with a different ID when I wanted one. I also tried making phone a unique key but I get a different error. here is the error message after setting phone as unique SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1234567890' for key 'phone'

here is the family table
<pre lang="SQL">CREATE TABLE IF NOT EXISTS `family` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `headofhousehold_firstname` varchar(20) NOT NULL,
  `headofhousehold_lastname` varchar(20) NOT NULL,
  `phone` varchar(15) NOT NULL,
  `address` text NOT NULL,
  `numofadults` int(11) NOT NULL,
  `numofkids` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ;


What I have tried:

I've tried making the 'phone' field a unique key, but I get a different error. How can I prevent duplicate data from being inserted into the 'family' table while still allowing multiple entries for the same firstname , lastname ? Any help would be appreciated.
Posted
Updated 9-Apr-23 18:02pm
Comments
Member 15627495 27-Mar-23 5:27am    
'primary key' are very important, the best for those is 'auto inc' because a 'auto inc' is never two or three.

remenber a phone number can change, using a phone number as 'primary key' is kind of 'dangerous', irrelevant.


to avoid 'duplicate' you can use 'distinct()' by sql.

or before an 'insert' statement you must retrieve the 'primary key' .
and use this parameter as 'foreign key' in the coming 'insert query'
CHill60 28-Mar-23 8:05am    
You are correct about the phone number point but "the best for those is 'auto inc' because a 'auto inc' is never two or three." doesn't make sense. Also "to avoid 'duplicate' you can use 'distinct()' by sql." is incorrect - that only works when extracting the data and would not work in this case because each record has a different Id so is by definition, distinct.
Finally "and use this parameter as 'foreign key' in the coming 'insert query'" is incorrect - there are no foreign key relationships here

The insertOrIgnore function will ignore duplicates - but you are not passing in sufficient information for it to determine that there is a duplicate - you are not including the id of the existing record.

I would suggest querying for the item first (based on the input data) then you can use UpdateOrCreate, including the Id. An example here: how-to-use-the-query-builder-updateorinsert-method[^]

As Member 15627495 says - a phone number can change so it is not a good candidate for a Primary key. The rest of their comment is not correct however.
 
Share this answer
 
I solved this myself. by checking if a record with the same phone number already exists in the family table and only inserting the data if it doesn't exist
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900