根据下拉选择显示特定的MySQL行 [英] Show a specific MySQL row based on dropdown selection

查看:88
本文介绍了根据下拉选择显示特定的MySQL行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含下拉字段的表单

I created a form that includes a dropdown field

<select name="locationselect" id="locationselect" tabindex="7">
  <option value="Location1">Location 1</option>
  <option value="Location2">Location 2</option>
  <option value="Location3">Location 3</option>
  <option value="Location4">Location 4</option>
</select>

提交后,我想从下拉列表中拉出他们选择的位置,并从我的MySQL数据库打印特定行,该行将向他们显示地址.因此,如果他们选择位置1,它将显示:

Upon submission I want to pull the location they selected from the dropdown and print a specific row from my MySQL database that would show them an address. So if they select Location 1 it would show:

Company Name 
1234 ABC Street
New York, NY 12345

但是,如果他们选择位置2,它将显示:

But if they select Location 2 it would show:

Other Company
5678 XYZ Street 
San Francisco, CA 12345 

依此类推,针对99个不同的位置.

And so on for 99 different locations.

这是我的开始,但是我缺少定义数组$ fulladdress的变量-我是MySQL的新手,所以我什至不知道在Select之后要放什么?有行号吗?我可以放在第一列的内容中还是什么类型的ID?

Here's what I started with but I'm missing a variable defining the array $fulladdress - I am new to MySQL so I'm not even sure what to put after Select? Is there a row number or can I put the contents of the first column or what type of ID?

switch($_GET['locationselect']){

case 'Location1':
   mysql_query("SELECT ____ FROM locations");
   break;
case 'Location2':
   mysql_query("SELECT ____ FROM locations");
   break;
  }
  while($row = mysql_fetch_array($fulladdress))
  {
  echo ($row['PlaceName']." Office Building<br>".$row['Address']."<br>".$row['City'].", CA      
  ".$row['Zip']."<br><br>");
  }

任何有关如何解决此问题的帮助将不胜感激.我知道我的代码很乱,但是我希望你能明白我要做什么.

Any help for how to solve this problem would be greatly appreciated. I know my code is messy but I'm hoping you can get the idea of what I'm trying to do.

谢谢!!

推荐答案

我不太确定要使用case语句,您可以做的是参数化查询.这样就可以了:

I'm not too sure about using the case statement, what you can do is a parameterised query. So it would be:

mysql_query("Select fulladdress from Location where location ='" . $location . "'");

使用下拉值,可以将其传递到$location变量中. 但是,如果要在搜索框中显示这么多的值.您可能想研究类似 jQuery Autocomplete 之类的东西.当然,您在转义输入之后.

Using the dropdown value, you can pass that into the $location variable. But if you're displaying so many values in a search box. You might want to look into something like jQuery Autocomplete. Of course after you've escaped the input.

上述方法不是很安全,您应该真正使用mysqli.并使用类似预准备语句的内容:

The above method isn't very secure, you should really use mysqli. And use something like prepared statements:

$stmt = $dbConnection->prepare('SELECT * FROM locations WHERE name = ?');
$stmt->bind_param('s', $name);

有关更多信息,请参见 SQL注入

For more information check this post on SQL Injection

这篇关于根据下拉选择显示特定的MySQL行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆