开发者

how to post two field values in one variable

I have two fields

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />

Lastname: <input type="text" name="lastname" />

Age: <input type="text" name="age" />
<input type="submit" />
</form>

how to post 开发者_如何学编程name and lastname in one variable meaning in one field of database is it

<?php
    $name=$_post['firstname']['lastname'];
?>


Actually you have three fields. Use string concatenation (or implode):

$name = $_POST['firstname'] . ' ' . $_POST['lastname'];

And don't forget to use mysql_real_escape_string (or what @ThiefMaster says) if you store the values in a database. Never trust user input.


Just concatenate the two values e.g.

<?php
    $name = $_POST['firstname'] . $_POST['lastname'];
?>


keep an array, and serialize it to store it.

$name['firstname']=$_post['firstname'];

$name['lastname']=$_post['lastname'];

//storage and retrieval methods 
$stored_name = serialize($name);

$name = unserialize($stored_name);

This way you don't lose the functionality of having the variables separate in an array, and you can always concatenate them later for display if you need to.


You can give the text inputs the same name with []

Firstname: <input type="text" name="name[]" />

Lastname: <input type="text" name="name[]" />

then you can

$name = $_POST['name'][0].$_POST['name'][1];

but i would prefer

$name=$_post['firstname'] . ' ' . $_post['lastname'];


This One will help You...! I also implemented this and it works...!

Firstname: <input type="text" name="firstName" />

Lastname: <input type="text" name="lastName" />

$fullname = $_post['firstName']. ' ' .$_post['lastName'];


$name = $firstname . " " . $lastname;

Then post $name in whatever field you want.


I had this same problem, i have a form with the name of the person reporting the issue and it takes the first name and the last name from my database of users and adds them together, but then when it came time to post both names to the database it would only post the first name. my solution was to first of all call the first name and last name from the database of users, then i called just the first name and last name and concat them together to produce reportername.

so this is the first part of the code calling for the user details i require for the form:

// Select the member from the users table
$sql = "SELECT * FROM users WHERE username='$log_username' AND activated='1' LIMIT 1";
$user_query = mysqli_query($db_conx, $sql);
// Now make sure that user exists in the table
$numrows = mysqli_num_rows($user_query);
if($numrows < 1){
echo "That user does not exist or is not yet activated, press back";
//    exit();   
}
// Fetch the user row from the query above
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$profile_id = $row["id"];
$first_name = $row["First_Name"];
$last_name = $row["Last_Name"];
$userlevel = $row["userlevel"];


}

Next i Concat the first name and last name:

$reporter_sql = "SELECT CONCAT (First_name,' ', Last_name) AS reportername FROM users WHERE username='$log_username' AND activated='1' LIMIT 1";
$reporter_results = mysqli_query($db_conx, $reporter_sql);
       while ($row = mysqli_fetch_array($reporter_results, MYSQLI_ASSOC)){
       $reportername = $row['reportername'];
       }

then you can post it to your database:

$reportername = mysqli_real_escape_string($db_conx, $reportername);

$sql = "INSERT INTO yourform  (`reportedby`) Value ('$reportername')";

I have striped my code down so it gives you an idea and I'm sure coders with more experience could tell you a simpler way to do this i just know it worked for me.


A shortcut way to concatenate variables is this:

$name = "$_POST[first_name] $_POST[last_name]"


General comment. A good proportion of the world don't have first and last names.

Better practice is just to ask for "Name", and stick it in one field.

If you must split 'em, then "given name" and "family name" are better labels.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜