MySQL查询的预先准备的列名不起作用

时间:2015-01-17 18:37:22

标签: javascript php arrays forms

我有这个PHP文件应该通过表单提交给它的所有信息,并将它放在MySQL数据库中。现在,我正处于确保我拥有所有信息的阶段,并且我正在准备"insert into (" . $var . ")"类型的查询。

数组:

$merged_arrays = array( 
    $sales_rep = strtoupper($_POST['sales_rep']),
    $c_first_name = strtoupper($_POST['c_first_name']),
    $c_last_name = strtoupper($_POST['c_last_name']),
    $c_address = strtoupper($_POST['c_address']),
    $c_zip = strtoupper($_POST['c_zip']),
    $deal_number = strtoupper($_POST['deal_number']),
    $deal_stock_number = strtoupper($_POST['deal_stock_number']),
    $deal_selling_model = strtoupper($_POST['deal_selling_model']),
    $deal_new_status = strtoupper($_POST['deal_new_status']),
    $deal_dh = strtoupper($_POST['deal_dh']),
    $deal_finance_amount = strtoupper($_POST['deal_finance_amount']),
    $deal_pack = strtoupper($_POST['deal_pack']),
    $deal_retro = strtoupper($_POST['deal_retro']),
    $deal_holdback = strtoupper($_POST['deal_holdback']),
    $sales_rep2 = strtoupper($_POST['sales_rep2']) or $sales_rep2 = null,
    $deal_trade_in_model_1 = strtoupper($_POST['deal_trade_in_model_1']),
    $deal_trade_in_amount_1 = strtoupper($_POST['deal_trade_in_amount_1']),
    $deal_trade_in_model_2 = strtoupper($_POST['deal_trade_in_model_2']),
    $deal_trade_in_amount_2 = strtoupper($_POST['deal_trade_in_amount_2']),
    $deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
    $deal_warrantee = strtoupper($_POST['deal_warrantee']),
    $deal_gap = strtoupper($_POST['deal_gap']),
    $deal_etch = strtoupper($_POST['deal_etch']),
    $deal_maintenence = strtoupper($_POST['deal_maintenence']),
    $deal_gross = strtoupper($_POST['deal_gross']));

补充数组: 这是数组"字典"将php varibles的所有翻译存储到表中的文字列名称。

$column_dictionary = array(
    $sales_rep = "Sales Rep 1",
    $c_first_name = "Customer First Name",
    $c_last_name = "Customer Last Name",
    $c_address = "Street Address",
    $c_zip = "Zip",
    $c_city = "City",
    $c_state = "State",
    $deal_number = "Deal Number",
    $deal_stock_number = "Stock #",
    $deal_selling_model = "Model",
    $deal_new_status = "New/Used",
    $deal_dh = "Destination and Handling Amount",
    $deal_finance_amount = "Financing Amount",
    $deal_pack = "Pack Amount",
    $deal_retro = "Retro Amount",
    $deal_holdback = "Holdback Amount",
    $sales_rep2 = "Sales Rep 2",
    $deal_trade_in_model_1 = "Trade-In Vehicle 1 Description",
    $deal_trade_in_amount_1 = "Trade-In Vehicle 1 Amount",
    $deal_trade_in_model_2 = "Trade-in Vehicle 2 Description",
    $deal_trade_in_amount_2 = "Trade-in Vehicle 2 Amount",
    $deal_reserve_amount = "Reserve Amount",
    $deal_warrantee = "Warrantee Amount",
    $deal_gap = "Gap Amount",
    $deal_etch = "Etch Amount",
    $deal_maintenence = "Maintenance Amount",
    $deal_gross = "Gross Amount");

While循环: 循环评估数组$merged_arrays以查看是否有任何内容。如果有内容,则应将其置于变量$columms中。

$columns = "";
$i = 0;
while($i < 26) {
    if (!$merged_arrays[$i] == false) {
        $columns = $columns . "`" . $column_dictionary[$i] . "`, ";
    }
    $i = $i + 1;
}

问题: 当我运行此项时,使用带有值的表单提交,$deal_trade_in_model_2会一直显示在列变量中(以及它的兄弟,$deal_trade_in_amount_2),无论是否在表单中输入或不。永远不会列出$deal_maintenence以及$deal_gross,即使我运行javsacript函数以确保它们全部设置为&#34; $ 0&#34; (因为它是一个货币领域。在过去的几个小时里,我已经尝试了几乎所有我能想到的东西,我真的需要了解我做错了什么,所以我不能继续做同样的错误。

我已经将页面的JS小提琴附加了它发送给它的表格,以及所有的JS和CSS。

http://jsfiddle.net/23smyy8g/

在回答这个问题时,请担任教师的角色。我喜欢从错误中吸取教训。 :)

1 个答案:

答案 0 :(得分:0)

命名数组使用箭头符号。

$myArray = array(
     'sales_rep' => 'Sales rep name',
     'company' => 'Company Name'
);

修改

您可以使用foreach循环来避免处理增量步骤。看起来你的if语句也会导致问题。

if (!$merged_arrays[$i] == false)

! not运算符的优先级高于==比较,因此这不是列,并检查是否为false,这很可能不是您正在尝试的。我假设您希望检查它是否为空/假,可以使用空函数。如果您只想检查它是否为假,请使用!=运算符,如下所示:

if ($merged_arrays[$i] != false)

另请注意,.=$str = $str . 'extra'相比更简洁。

$columns = "";
foreach ($merged_arrays  as $key => $column)
    if (!empty($column)) {
        $columns .= "`" . $column_dictionary[$key] . "`, ";
    }
}

进一步编辑

您应该按以下方式构建数组:

array(
    'key1' => value1,
    'key2' => value2
)

您的数组实际上是保存变量赋值的结果。