如何获取所选数据列表的ID

时间:2020-12-13 07:28:20

I have just figured out how to populate a datalist element from mysql. I need to get the associated ID that corresponds to the selected value to submit to another page. I can submit the value, but I need the ID. I hope it is a simple syntax error, but I can't figure it out. Thanks in advnace.

我刚刚知道如何从mysql中填充datalist元素。我需要获得对应于要提交到另一个页面的所选值的关联ID。我可以提交这个值,但是我需要ID,我希望这是一个简单的语法错误,但是我搞不清楚。由于advnace。

<?php

require 'connect_mysqli.php';

$sql = "SELECT street FROM streets";
$result = mysqli_query($con, $sql) or die ("Error " . mysqli_error($con));
?>
<form action="new.php" name="test" method = "post">
<datalist id="street" name="streets">
    <?php while($row = mysqli_fetch_array($result)) { ?>
        <option value="<?php echo $row['street']; ?>"><?php echo $row['street_id']; ?></option>
    <?php 
    } 
?>

</datalist>
<input type="text" name="street" id="test" autocomplete="off" list="street">
<input type="submit" value="Submit">
<?php 
mysqli_close($con); 
?>

3 个解决方案

#1


1  

Also I think you are doing wrong to submit value of street. Change it to this

我也认为你提交街头价值是错误的。改变到这个

  <option value="<?php echo $row['street_id']; ?>"><?php echo $row['street']; ?></option>

It will display street name and submit street id And get value like this

它将显示街道名称和提交街道id,并获得如下值

$_post["streets"];// I haven't used datalist but if works it would be this

#2


2  

Look at your query:

看看你的查询:

$sql = "SELECT street FROM streets";

"I hope it is a simple syntax error"

"我希望是一个简单的语法错误"

You didn't make a syntax "error", you just didn't select the street_id column in your query, that's why it's not showing in the dropdown, in regards to $row['street_id'].

您没有创建语法“error”,您只是没有在查询中选择street_id列,这就是为什么它没有显示在下拉菜单中,关于$row['street_id']。

You need to add it in your query:

您需要在查询中添加:

$sql = "SELECT street, street_id FROM streets";

N.B.: Column selection are separated by commas. You can further add to the query if you wish, but make sure there isn't a trailing comma following the last column chosen.

注意::列选择用逗号分隔。如果您愿意,可以进一步添加到查询中,但是请确保在最后选择的列后面没有一个逗号。

I.e: This would FAIL:

我。艾凡:这将会失败:

$sql = "SELECT street, street_id, col3, FROM streets";

However, $row['street_id'] and $row['street_ID'] are two different animals altogether here. So, make sure that that is the letter-case used. They are case-sensitive when it comes to looping over column names, and that would trigger an error when checking for them, being something to the effect of an unexisting column.

然而,$row['street_id']和$row['street_id']是两个完全不同的动物。所以,请确保这是使用的字母框。当涉及到对列名进行循环时,它们是区分大小写的,这将在检查它们时触发一个错误,这是不存在的列的影响。

Sidenote:

旁注:

I didn't see a closing </form> tag in your posted code. Make sure it's part of your real code.

我在您所发布的代码中没有看到结束标记。确保它是你真实代码的一部分。


Reference:

参考:

#3


1  

You just forgot to include the street_id column in the columns you are selecting from the streets table.

您只是忘记在您从streets表中选择的列中包含street_id列。

Change this line:

改变这条线:

$sql = "SELECT street FROM streets";

to this line:

这条线:

$sql = "SELECT street, street_id FROM streets";

#1


1  

Also I think you are doing wrong to submit value of street. Change it to this

我也认为你提交街头价值是错误的。改变到这个

  <option value="<?php echo $row['street_id']; ?>"><?php echo $row['street']; ?></option>

It will display street name and submit street id And get value like this

它将显示街道名称和提交街道id,并获得如下值

$_post["streets"];// I haven't used datalist but if works it would be this

#2


2  

Look at your query:

看看你的查询:

$sql = "SELECT street FROM streets";

"I hope it is a simple syntax error"

"我希望是一个简单的语法错误"

You didn't make a syntax "error", you just didn't select the street_id column in your query, that's why it's not showing in the dropdown, in regards to $row['street_id'].

您没有创建语法“error”,您只是没有在查询中选择street_id列,这就是为什么它没有显示在下拉菜单中,关于$row['street_id']。

You need to add it in your query:

您需要在查询中添加:

$sql = "SELECT street, street_id FROM streets";

N.B.: Column selection are separated by commas. You can further add to the query if you wish, but make sure there isn't a trailing comma following the last column chosen.

注意::列选择用逗号分隔。如果您愿意,可以进一步添加到查询中,但是请确保在最后选择的列后面没有一个逗号。

I.e: This would FAIL:

我。艾凡:这将会失败:

$sql = "SELECT street, street_id, col3, FROM streets";

However, $row['street_id'] and $row['street_ID'] are two different animals altogether here. So, make sure that that is the letter-case used. They are case-sensitive when it comes to looping over column names, and that would trigger an error when checking for them, being something to the effect of an unexisting column.

然而,$row['street_id']和$row['street_id']是两个完全不同的动物。所以,请确保这是使用的字母框。当涉及到对列名进行循环时,它们是区分大小写的,这将在检查它们时触发一个错误,这是不存在的列的影响。

Sidenote:

旁注:

I didn't see a closing </form> tag in your posted code. Make sure it's part of your real code.

我在您所发布的代码中没有看到结束标记。确保它是你真实代码的一部分。


Reference:

参考:

#3


1  

You just forgot to include the street_id column in the columns you are selecting from the streets table.

您只是忘记在您从streets表中选择的列中包含street_id列。

Change this line:

改变这条线:

$sql = "SELECT street FROM streets";

to this line:

这条线:

$sql = "SELECT street, street_id FROM streets";