Akshay Sargar
Akshay Sargar

Reputation: 47

Store 2 inputs having same name into 1 'address' column of the database

I have two address inputs 'address1' and 'address2' having the same name as 'address[]' . I want to put the value of both the inputs in a single address column of the database but the problem is that data of only 2nd input is being stored into the database.

index.php

  $addressData = $_POST['address']; 
    foreach ($addressData as  $addressValue) {
              $query = "INSERT INTO `users` (`name` ,`address` , `birthdate` ,`age` , `coach` , `phone`,`email` ,`password`)
             VALUES ( '".mysqli_real_escape_string($link, $_POST['name'])."' ,  '".mysqli_real_escape_string($link, $addressValue)."' ,'".mysqli_real_escape_string($link, $_POST['birthdate'])."' ,'".mysqli_real_escape_string($link, $_POST['age'])."' , '".mysqli_real_escape_string($link, $_POST['coach'])."' , '".mysqli_real_escape_string($link, $_POST['phone'])."' , '".mysqli_real_escape_string($link, $_POST['email'])."' , '".mysqli_real_escape_string($link, $_POST['password'])."')";
                                 }
 <div class="form-group row">
    <label class="col-sm-2 form-control-label">Address1</label>
    <div class="col-sm-10">
      <input type="text" id="address" class="form-control" name="address[]" placeholder="Home address">
      <span class="fa fa-map-marker"></span>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-2 form-control-label">Address2</label>
    <div class="col-sm-10">
      <input type="text" id="address2" class="form-control" name="address[]" placeholder="City,Pincode....">
      <span class="fa fa-map-marker"></span>
    </div>
  </div>

Upvotes: 0

Views: 81

Answers (3)

Cookie Ninja
Cookie Ninja

Reputation: 1184

For storing multiple values in a single column, create a new table that stores address, and link it to the main table.

users(pid, address)
address(fkid, address)

From the sample, link the 2 tables by referencing fkid as a foreign key of the address to the primary key of the table. In these way, you can store multiple the same datas in a particular ID.

So taken from my sample, to insert it after your the insertion of the user table, get the created id by:

$last_insert_id = mysqli_insert_id();

Then, insert the addresses:

foreach($addressData as $addressValue) {
    mysql_query("INSERT INTO user(id, address) VALUE($last_insert_id, $addressValue);
}

It is useful, like 1 user having multiple email accounts, or multiple groups that joins. And can be use in a bunch of ways, rather than storing it as a text separated with commas.

Upvotes: 0

Allan Empalmado
Allan Empalmado

Reputation: 943

It is because you do insert inside the foreach loop. If you want the address to be saved on a single table, you might want to combine the address first and do insert outside the foreach loop. See the updated code below

$addressData = $_POST['address']; 

$address = "";
foreach ($addressData as  $addressValue) {
    $address .= "\n" . $addressValue;
}               
$query = "INSERT INTO `users` (`name` ,`address` , `birthdate` ,`age` , `coach` , `phone`,`email` ,`password`)

           VALUES ( '".mysqli_real_escape_string($link, $_POST['name'])."' ,
                    '".mysqli_real_escape_string($link, $address)."' ,
                    '".mysqli_real_escape_string($link, $_POST['birthdate'])."' ,
                    '".mysqli_real_escape_string($link, $_POST['age'])."' ,
                    '".mysqli_real_escape_string($link, $_POST['coach'])."' ,
                    '".mysqli_real_escape_string($link, $_POST['phone'])."' ,
                    '".mysqli_real_escape_string($link, $_POST['email'])."' ,
                    '".mysqli_real_escape_string($link, $_POST['password'])."'   )";

Upvotes: 1

Roy Stijsiger
Roy Stijsiger

Reputation: 309

The name attribute specifies the name of an element.

The name attribute is used to reference elements in a JavaScript, or to reference form data after a form is submitted.

You should not have the same name for 2 elements.

Why just not call it Adress1 and Adress2

Upvotes: 0

Related Questions