php 如何在一个变量中发布两个字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5909598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:51:24  来源:igfitidea点击:

how to post two field values in one variable

php

提问by Francis D Cunha

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'];
?>

采纳答案by trickwallett

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.

如果您必须拆分它们,那么“名字”和“姓氏”是更好的标签。

回答by Felix Kling

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

实际上你有三个字段。使用字符串连接(或implode):

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

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

如果您将值存储在数据库中,请不要忘记使用mysql_real_escape_string(或@ThiefMaster所说的)。永远不要相信用户输入。

回答by planetjones

Just concatenate the two values e.g.

只需连接两个值,例如

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

回答by Mild Fuzz

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.

这样您就不会失去在数组中分隔变量的功能,并且您可以随时将它们连接起来以供显示(如果需要)。

回答by Dukeatcoding

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'];

回答by Saddam Hussain

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'];

回答by frosty

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.

我已经把我的代码条带化了,所以它给了你一个想法,我相信有更多经验的程序员可以告诉你一个更简单的方法来做到这一点,我只知道它对我有用。

回答by Neoaptt

A shortcut way to concatenate variables is this:

连接变量的一种快捷方式是:

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

回答by check123

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

Then post $name in whatever field you want.

然后在您想要的任何字段中发布 $name 。