简体   繁体   English

PHP / MySQL:获取特定ID的名称

[英]PHP/MySQL: Get name for specific ID

I am a beginner to MySQL and Im trying to retrieve a name for a specific ID. 我是MySQL的初学者,我正在尝试检索特定ID的名称。

Here is how my database looks like: 这是我的数据库的样子:

I have a table of contacts which contains ID, first and last name. 我有一张包含ID,名字和姓氏的联系人表。

I want to retrieve the first and last name for a specific ID in the same form. 我想以相同的形式检索特定ID的名字和姓氏。

So I want to end up with a form of options and the option value will be the first name for each ID. 因此,我想以一种选项形式结束,选项值将是每个ID的名字。

For example: 例如:

<form>
<select name="users">
<option value="">Select a person:</option>
<option value="5"> <?php echo "$first $last" ?> </option>
<option value="10"> <?php echo "$first $last" ?> </option>
<option value="15"> <?php echo "$first $last" ?> </option>
</select>
</form>

The values are the ID's. 值是ID。

and my PHP: 和我的PHP:

<?php   include("connection.php");

$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");

$i++;
}

?>

in my actual example I get the first and last name of the last's person in the database only. 在我的实际示例中,我仅获得数据库中姓氏的名字和姓氏。

So how can I retrieve the first and last name for specific ID's of which I assign in my form (?) 那么,如何检索在表单中分配的特定ID的名字和姓氏(?)

This is because your while loop is overwriting $first and $last every single time. 这是因为您的while循环每次都会覆盖$ first和$ last。 Try 尝试

$users = array();

while ($row = mysql_fetch_assoc($result)) { 
  $users[] = $row;
}

And in HTML (EDITED): 并在HTML(已编辑)中:

<form>
  <select name="users">
    <option value="">Select a person:</option>
    <?php foreach($users as $row): ?>
      <option value="<?php echo $row['id']; ?>"> <?php echo $row['first'] . " " .  $row['last']; ?> </option>
    <?php endforeach; ?>
  </select>
</form>

Assuming your contacts table has a primary key field, you'd use THAT field to put into your <option> tags as the value. 假设您的联系人表具有主键字段,则可以使用THAT字段将其作为值放入<option>标记中。 Then you've got a 1:1 correspondence between a particular <option> and a record in the database. 然后,您在特定的<option>和数据库中的记录之间建立了1:1的对应关系。 If you go by name alone, you'll end up (say) 5 "John Smith" entries and it would be impossible to tell which one you want. 如果仅凭名字来命名,您将最终得到(说)5个“ John Smith”条目,就不可能说出您想要哪个。

So, make your query be: 因此,使您的查询为:

SELECT id, firstname, lastname
FROM ...

and then: 接着:

    while ($row = mysql_fetch_assoc($result)) {
       echo <<<EOL
<option value="$row[id]">$row[firstname] $row[lastname]</option>

EOL;
    }
$users = $_POST['users'];
$query = 
"SELECT 
    ID, FIRSTNAME, LASTNAME 
FROM 
    CONTACTS 
WHERE 
    ID = '$users'";

Of course, while this works, it is a classic example of a SQL injection vulnerability. 当然,尽管这有效,但这是SQL注入漏洞的经典示例。 You'll want to either sanitize the input or use a prepared statement and binding variables. 您需要清理输入或使用准备好的语句和绑定变量。

You can do 你可以做

$query="SELECT * FROM contacts WHERE id = '$id'";

A little googling will go a long way. 稍作谷歌搜索将大有帮助。 Read this and this . 阅读这个这个

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

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