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

时间:2015-01-22 14:26:30

标签: php mysql forms if-statement

我正在用PHP / MYSQL编写一个简单的Web程序(由于限制,必须使用mysql查询而不是mysqli或PDO)。 HTML表必须根据用户表单输入显示我的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'");
    }

?>

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

编辑,根据@ 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未定义的错误。它是合法的&#34;将变量用于行名?

2 个答案:

答案 0 :(得分:1)

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

<?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 =&#39; 401(k)&#39;

     

SELECT * FROM sampletable WHERE Organization_match_participants_contribution =&#39;雇主匹配贡献(即前6%薪酬的50%)&#39; AND PT_401k =&#39; 401(k)&#39;

答案 1 :(得分:0)

首先,做:

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

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

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

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