简体   繁体   中英

Inserting multiple fields from a form into single field in database

I am using following code to ask user to enter his first name, middle name and last name.

<label>
  <span>First name</span>
  <input type="text" size ="25" placeholder="Your Name" class="input_text"onfocus="if(this.value==this.defaultValue)this.value=''"    
    onblur="if(this.value=='')this.value=this.defaultValue" 
    value="" name="name" id="name"/>
</label>

<label>
  <span> Middle Name</span>
  <input type="text" size ="25 class="input_text" placeholder="Father's name"  name="middle name" onfocus="if(this.value==this.defaultValue)this.value=''"    
    onblur="if(this.value=='')this.value=this.defaultValue" 
    value="" id="middle name"/>
</label>

<label>
  <span>Last Name</span>
  <input type="text" size ="25" class="input_text" placeholder="Surname"  name="last name" onfocus="if(this.value==this.defaultValue)this.value=''"    
    onblur="if(this.value=='')this.value=this.defaultValue" 
    value="" id="last name"/>
</label>

I want all the 3 first name, middle name and last name to go into one single column called name in database.

FYI I am using wamp and have created my database in phpmyadmin. I want to insert all 3 fields into name using a php script. How do I go about?

You can concatenate the fields before inserting them into the database;

$name = '';
if(isset($_POST['first_name'])){
    $name .= $_POST['first_name'];
}
if(if(isset($_POST['middle_name'])){
    //add a space before
    $name .= ' ' .$_POST['middle_name'];
}
if(isset($_POST['last_name'])){
    //add a space before
    $name .= ' ' . $_POST['last_name'];
}

//now you have your full name in the $name variable, you can insert it into the database

Note: You cannot use space in your form input names.You should use an underscore if you want two words.

Wrong:

<input type="text" name="middle name">

Correct:

<input type="text" name="middle_name">

first change your middle and last name attribue, do not keep space between namig. ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

<input type="text" size ="25" class="input_text" placeholder="Father's name"  name="middle name" onfocus="if(this.value==this.defaultValue)this.value=''"    
    onblur="if(this.value=='')this.value=this.defaultValue" 
    value="" id="m_name"/>

<input type="text" size ="25" class="input_text" placeholder="Surname"  name="last name" onfocus="if(this.value==this.defaultValue)this.value=''"    
    onblur="if(this.value=='')this.value=this.defaultValue" 
    value="" id="l_name"/>

You can make it a single string for displaying full name at front end side and insert it into name field as:

<?php
//your connection
$f_name=$_POST['name'];
$m_name=$_POST['m_name'];
$l_name=$_POST['l_name'];
$full_name=$f_name." ".$m_name." ".$l_name;
mysqli_query($con,"insert into table_name set name='$full_name'");
?>

Change your name attribute to the below mentioned $_POST[] variable name.

<?php

//your connection

$f_name = mysqli_real_escape_string($con, $_POST['name']);
$m_name = mysqli_real_escape_string($con, $_POST['m_name']);
$l_name = mysqli_real_escape_string($con, $_POST['l_name']);

$full_name = $f_name . " " . $m_name . " " . $l_name;

$sql = "INSERT INTO yourTableName (tableColumnName) VALUES ('".$fullname."')";
$res = mysqli_query($con, $sql);

?>

Hope this helps you out.

you can use json_encode for $_POST variable to get json string and store it in db.

To get the original array from the stored json string, you can use json decode , so that no information is lost.

Here is an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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