简体   繁体   English

使用 PHP 从 mysql 动态 select 表

[英]Dynamically select a table from mysql using PHP

I created an html that has a formulary that let you choose between four different brand cars:我创建了一个 html,它有一个让您可以在四种不同品牌的汽车之间进行选择的公式:

<select name="brand">
  <option value = "hyundai"> Hyundai </option>
  <option value = "renault"> Renault </option>
  <option value = "ford"> Ford </option>
  <option value = "fiat"> Fiat </option>
</select>

Each of these options correspond to one table in a mySQL database, and I want to dynamically choose any of these tables, given the users choice.这些选项中的每一个都对应于 mySQL 数据库中的一个表,并且我想根据用户的选择动态选择这些表中的任何一个。 So I have the next code:所以我有下一个代码:

$brand = $_POST['brand'];

$database = "cars"
if ($brand == "hyundai") {
  $table == "ref_hyundai";
} else if ($brand == "renault") {
  $table == "ref_renault";
} else if ($brand == "ford") {
  $table == "ref_ford";
} else {
  $table == "ref_fiat";
}

However, when I check the var/apache2/errors, I see this:但是,当我检查 var/apache2/errors 时,我看到:

PHP Notice:  Undefined variable: table 

Which correspond to the call of the $table variable later in the code (in the query specifically).这对应于代码后面对 $table 变量的调用(特别是在查询中)。

Seems something is wrong, but I don´t know what could it be.似乎有什么问题,但我不知道会是什么。

Any help would be appreciated it.任何帮助将不胜感激。

ALSO: This is a learning example, I don´t need it to be mysql injection proof还:这是一个学习示例,我不需要它是 mysql 注入证明

When you use == you are comparing $table with something, not assigning the value.当您使用==时,您将$table与某些东西进行比较,而不是分配值。 You need to replace == by = in $table declarations.您需要在$table声明中将==替换为=

$brand = $_POST['brand'];

$database = "cars"
if ($brand == "hyundai") {
  $table = "ref_hyundai";
} else if ($brand == "renault") {
  $table = "ref_renault";
} else if ($brand == "ford") {
  $table = "ref_ford";
} else {
  $table = "ref_fiat";
}

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

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