繁体   English   中英

重构循环mySQL和PHP代码

[英]Refactoring to loop mySQL & PHP code

目前,我有一个名为字段的关联数组,该数组存储我的HTML输入字段中的所有$ _POST变量。 当我尝试将值绑定到mySQL数据库时,将使用关联数组变量。以相同的方式,我也具有与mySQL数据库的列标题相同的名称。 因此,这非常繁琐且漫长。 有没有办法遍历各列并为关联数组分配另一个循环的值? 请检查下面的示例,并在此先感谢您的帮助。

     try {
                $insertSql = "INSERT INTO tableExample";
                $sqlCols = " (
                    a,
                    b,
                    c,
                    d,
                    e,
                    f,
                    g,
                    h,
                    i,
                    j,
                    k,
                    l,
                    m,
                    n,
                    o,
                    p,
                    q,
                    r,
                    s,
                    t,
                    u,
                    v,
                    w,
                    x
                )";
                $result = $db->prepare($insertSql . $sqlCols . " VALUES (
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?)");
                $result->bindValue(1,$fields['a'],PDO::PARAM_STR);
                $result->bindValue(2,$fields['b'],PDO::PARAM_STR);
                $result->bindValue(3,$fields['c'],PDO::PARAM_STR);
                $result->bindValue(4,$fields['d'],PDO::PARAM_STR);
                $result->bindValue(5,$fields['e'],PDO::PARAM_STR);
                $result->bindValue(6,$fields['f'],PDO::PARAM_STR);
                $result->bindValue(7,$fields['g'],PDO::PARAM_STR);
                $result->bindValue(8,$fields['h'],PDO::PARAM_STR);
                $result->bindValue(9,$fields['i'],PDO::PARAM_STR);
                $result->bindValue(10,$fields['j'],PDO::PARAM_STR);
                $result->bindValue(11,$fields['k'],PDO::PARAM_STR);
                $result->bindValue(12,$fields['l'],PDO::PARAM_STR);
                $result->bindValue(13,$fields['m'],PDO::PARAM_STR);
                $result->bindValue(14,$fields['n'],PDO::PARAM_STR);
                $result->bindValue(15,$fields['o'],PDO::PARAM_STR);
                $result->bindValue(16,$fields['p'],PDO::PARAM_STR);
                $result->bindValue(17,$fields['q'],PDO::PARAM_STR);
                $result->bindValue(18,$fields['r'],PDO::PARAM_STR);
                $result->bindValue(19,$fields['s'],PDO::PARAM_STR);
                $result->bindValue(20,$fields['t'],PDO::PARAM_STR);
                $result->bindValue(21,$fields['u'],PDO::PARAM_STR);
                $result->bindValue(22,$fields['v'],PDO::PARAM_STR);
                $result->bindValue(23,$fields['w'],PDO::PARAM_STR);
                $result->bindValue(24,$fields['x'],PDO::PARAM_STR);
                $result->execute();
            } catch (Exception $e) {
                echo "Unable to store data";
                echo $e->getMessage();
                exit;
            }           

快速煮熟可能的解决方案。 在尝试之前,请记住,它未经测试,并且这里可能存在一些语法错误(我端没有PDO ...实际上,也没有php)。

它的作用是定义两个单独的映射:一个用于“数据库列到索引”,另一个用于“数据库列到值”。

我不确定这是否可以解决您的问题,但这是一个起点。

<?php
try 
{
    //Define your columns and indexes...
    $fields=['col_a' => 1, 'col_b' => 2, 'col_c' => 3, 'col_d' => 4];

    //Let this be your post data. Notice how the index are the same as above.
    $data=['col_a' => "Value a", "col_b" => 33, "col_c" => "Value C", "col_d" => 12];

    //Prepare the statement....
    $sqlCols=null;
    $sqlValues=null;

    foreach($fields as $key => $value) 
    {
        $sqlCols.=$key.',';
        $sqlValues.='?,';
    }

    $result = $db->prepare("INSERT INTO tableExample (".substr($sqlCols, 0, -1).") VALUES (".substr($sqlValues, 0, -1).")";

    //Bind the values... Please, make sure the order of parameters is correct!.
    foreach($fields as $key => $index)
    {
        $result->bindValue($index, $data[$key], PDO::PARAM_STR);
    }

    $result->execute();
} 
catch (Exception $e) 
{
    echo "Unable to store data";
    echo $e->getMessage();
}
?>

再次,让我指出您正在通过post变量中的名称公开数据库结构。 我怀疑那是你想要做的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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