简体   繁体   中英

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:

<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. 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:

PHP Notice:  Undefined variable: table 

Which correspond to the call of the $table variable later in the code (in the query specifically).

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

When you use == you are comparing $table with something, not assigning the value. You need to replace == by = in $table declarations.

$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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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