Sujan Shrestha
Sujan Shrestha

Reputation: 1040

How to join multiple column values into one column while inserting

I have a table which looks like this:

ID | Name | Address

I have a form which looks like this:

  <input type="text" class="form-control" name="name"/>
      <input type="text" class="form-control" name="address"/>

My PHP Code:

    $name = $_POST['name'];
    $address = $_POST['address'];
    $sql = "Insert into staff(client_name,address) VALUES ('$client_name','$address')";
    $this->db->query($sql);

What i want to do is, join the values posted by user into one column.i.e if user types JOHN in Name field and USA in address field. I want to following in my database table

ID | Name | Address
1   | JOHN USA | USA

Upvotes: 1

Views: 99

Answers (5)

sayli bhagwat
sayli bhagwat

Reputation: 273

Use json_encode to encode the content and then store it in database field and when you retrieve the value then use json_decode

$form_data_json = json_encode( $_POST );
// store $form_data_json in database.

// When you want to get stored data, just fetch it from db. let it be stored in $fetch 
$original_post_array = json_decode( $fetch, true );

Upvotes: 0

arisalsaila
arisalsaila

Reputation: 429

Simply concatenate your $_POST['name'] with $_POST['address'] in $client_name before inserting to database. Like this :

     $client_name = $_POST['name'] .' '. $_POST['address'];
     $address = $_POST['address'];
     $sql = "Insert into staff(client_name,address) VALUES ('$client_name','$address')";
     $this->db->query($sql);

Upvotes: 0

Shibon
Shibon

Reputation: 1574

try this

$name = $_POST['name'];
$address = $_POST['address'];
$client_name = $name.' | '.$address;
$sql = "Insert into staff(client_name,address) VALUES   ('$client_name','$address')";
$this->db->query($sql);

Upvotes: 0

Suni
Suni

Reputation: 51

 $client_name = $_POST['name'];
 $address = $_POST['address'];
 $name = $client_name .' '. $address;
 $sql = "Insert into staff(client_name,address) VALUES ('$name','$address')";
 $this->db->query($sql);

Upvotes: 0

Bernd Buffen
Bernd Buffen

Reputation: 15057

use thomething like this:

$sql = "Insert into staff(client_name,address) VALUES ('$client_name $address','$address')";

Upvotes: 2

Related Questions