0

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?

4 Answers 4

1

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">
Sign up to request clarification or add additional context in comments.

2 Comments

DONE CORRECTION AS PER YOUR SUGGESTION! Always a blank is inserted in database. However If i comment out $name=' '; then execute mysqli_query($con,"insert into table_name set name='$name'"); it gives undefined symbol error
undefined index m_name, l_name etc; I have made name attributes as m_name, l_name and so on\
1

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'");
?>

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.