PHP / MYSQL - 用 IF、ELSE 定义变量?

PHP / MYSQL - define variables with IF, ELSE?

我正在用 PHP / MYSQL 编写一个简单的网络程序(由于限制,必须使用 mysql 查询而不是 mysqli 或 PDO)。 HTML table 必须根据用户表单输入显示与我的 MYSQL table 不同的值。

根据用户输入定义多个变量的最佳方法是什么?

我试过了

<?php
$taxcode = $_POST['taxcode'];

$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'root';

$database = 'database';
$table = 'sampletable';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

    if ($taxcode = '401(k)') {
    $planquery = ("SELECT * FROM sampletable WHERE PT_401k = '401(k)'");
    $orgmatchquery = ("SELECT * FROM sampletable WHERE Organization_match_participants_contribution = 'Employer Matching contribution (i.e., 50% of first 6% of pay)' AND PT_401k = '401(k)'");
    } elseif ($taxcode = '403(b)') {
    $planquery = ("SELECT * FROM sampletable WHERE PT_403b = '403(b)'");
    $orgmatchquery = ("SELECT * FROM sampletable WHERE Organization_match_participants_contribution = 'Employer Matching contribution (i.e., 50% of first 6% of pay)' AND PT_403b = '403(b)'");
    } elseif ($taxcode = '457') {
    $planquery = ("SELECT * FROM sampletable WHERE PT_457 = '457'");
    $orgmatchquery = ("SELECT * FROM sampletable WHERE Organization_match_participants_contribution = 'Employer Matching contribution (i.e., 50% of first 6% of pay)' AND PT_457 = '457'");
    }

?>

但它不会根据输入更改值。我做错了什么?

编辑,根据@Demodave 的回答 - 这似乎在朝着正确的方向发展,但不太奏效。

<?php

$taxcode = '401(k)';

    if ($taxcode == '401(k)' || $taxcode == '403(b)' || $taxcode == '457') {

    if($taxcode == '401(k)') {
        $where = "PT_401k"; 
    } else if($taxcode == '403(b)') {
        $where = "PT_403b";
    } else if($taxcode == '457') {
        $where = "PT_457";
    }

    echo $planquery = ("SELECT * FROM sampletable WHERE " . $where . " = '" . $taxcode . "'");
    echo "<br />" . $orgmatchquery = ("SELECT * FROM sampletable WHERE Organization_match_participants_contribution = 'Employer Matching contribution (i.e., 50% of first 6% of pay)' AND " . $where . " = '" . $taxcode . "'");   

    }

?>

获取 $where 未定义的错误。是否"legal"使用变量作为行名?

首先,通过做:

if ($taxcode = '401(k)') {

您正在为变量赋值。你需要写:

if ($taxcode == '401(k)') {

除此之外,您可能希望动态构建该查询。有帮助吗?

你有一些重复元素尝试这样做。

<?php

$taxcode = '401(k)'; // This is only an example for the POST variable.

if ($taxcode == '401(k)' || $taxcode == '403(b)' || $taxcode == '457') {

if($taxcode == '401(k)') {
    $where = "PT_401k"; 
} else if($taxcode == '403(b)') {
    $where = "PT_403b";
} else if($taxcode == '457') {
    $where = "PT_457";
}

echo $planquery = ("SELECT * FROM sampletable WHERE " . $where . " = '" . $taxcode . "'");
echo "<br />" . $orgmatchquery = ("SELECT * FROM sampletable WHERE Organization_match_participants_contribution = 'Employer Matching contribution (i.e., 50% of first 6% of pay)' AND " . $where . " = '" . $taxcode . "'");   

}

?>

输出:

SELECT * FROM sampletable WHERE PT_401k = '401(k)'

SELECT * FROM sampletable WHERE Organization_match_participants_contribution = 'Employer Matching contribution (i.e., 50% of first 6% of pay)' AND PT_401k = '401(k)'