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

PHP/MYSQL-使用IF、ELSE定义变量?,php,mysql,forms,if-statement,Php,Mysql,Forms,If Statement,我正在用PHP/MYSQL编写一个简单的web程序(由于限制,必须使用MYSQL查询,而不是mysqli或PDO)。HTML表必须根据用户表单输入显示与my MYSQL表不同的值 根据用户输入定义多个变量的最佳方法是什么 我试过了 <?php $taxcode = $_POST['taxcode']; $db_host = 'localhost'; $db_user = 'root'; $db_pwd = 'root'; $database = 'database'; $table =

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

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

我试过了

<?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'");
    }

?>

首先,通过执行以下操作:

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 . "'");   

}

?>

这看起来很有希望,但我遇到了错误:注意:未定义变量:第86行的/Applications/MAMP/htdocs/benchmark/step2.php中的where(看起来像是“where”变量)警告:mysql_num_rows()希望参数1是资源,第93行的/Applications/MAMP/htdocs/benchmark/step2.php中给出的布尔值我正在定义$where with='',问题是您是否包含if($taxcode='401(k)| |$taxcode='403(b)| |$taxcode='457')){行,否则如果taxcode不是那些数字中的一个,$where将永远不会被定义。是的;但是应该是无关的,因为用户输入是一个只有三个选项的选择下拉列表。我们确定$where(或任何变量)是否可以在这种情况下使用?在上面添加了一个编辑。它只是一个变量名,如果需要,请使用与$where不同的单词,但应出现相同的结果,请自行尝试上面的示例。是否确保正在比较(=)而未分配(=)项?发布更新的代码。确保表单的方法为Post。
<?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 . "'");   

}

?>