简体   繁体   English

如何在插入时将多个列值连接到一列中

[英]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: 我的PHP代码:

    $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.ie if user types JOHN in Name field and USA in address field. 我想要做的是,将用户发布的值加入一列。即,如果用户在“名称”字段中键入JOHN,在地址字段中键入USA。 I want to following in my database table 我想在我的数据库表中关注

ID | Name | Address
1   | JOHN USA | USA

使用这样的方法:

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

Simply concatenate your $_POST['name'] with $_POST['address'] in $client_name before inserting to database. 在插入数据库之前, $client_name$client_name$_POST['name']$_POST['address']起来。 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);
 $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);

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);

Use json_encode to encode the content and then store it in database field and when you retrieve the value then use json_decode 使用json_encode对内容进行编码,然后将其存储在数据库字段中,当您检索值时,请使用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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM