从表单到MySQL处理大量$ _POST的最有效方法是什么?

时间:2015-08-09 23:45:27

标签: php mysql

我有一张表格,我正在通过邮件检索数据。对于此示例,大约有15个字段。在这个应用程序的另一个页面上,将有50多个,我已经不敢以我的方式写出来。我怎么能把它缩小呢?

(另外,请忽略缺少消毒,稍后我会补充一下。)

if (($_GET['mode'] == "update") and isset($_GET['id']) and isset($_POST['who'])) {

    $who = $_POST['who'];       
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $phone1 = $_POST['phone1'];
    $phone2 = $_POST['phone2'];
    $phone3 = $_POST['phone3'];
    $email = $_POST['email'];
    $dob = $_POST['dob'];
    $ssn = $_POST['ssn'];
    $website = $_POST['website'];
    $checks = $_POST['checks'];

    $statements = array();

    array_push($statements, "update subcontractors set firstname='" . $firstname . "' where id=" . $who);
    array_push($statements, "update subcontractors set lastname='" . $lastname . "' where id=" . $who);
    array_push($statements, "update subcontractors set address='" . $address . "' where id=" . $who);
    array_push($statements, "update subcontractors set city='" . $city . "' where id=" . $who);
    array_push($statements, "update subcontractors set state='" . $state . "' where id=" . $who);
    array_push($statements, "update subcontractors set zip='" . $zip . "' where id=" . $who);
    array_push($statements, "update subcontractors set phone1='" . $phone1 . "' where id=" . $who);
    array_push($statements, "update subcontractors set phone2='" . $phone2 . "' where id=" . $who);
    array_push($statements, "update subcontractors set phone3='" . $phone3 . "' where id=" . $who);
    array_push($statements, "update subcontractors set email='" . $email . "' where id=" . $who);
    array_push($statements, "update subcontractors set dob='" . $dob . "' where id=" . $who);
    array_push($statements, "update subcontractors set ssn='" . $ssn . "' where id=" . $who);
    array_push($statements, "update subcontractors set website='" . $website . "' where id=" . $who);
    array_push($statements, "update subcontractors set checks='" . $checks . "' where id=" . $who);

    include "connect.php";
    foreach ($statements as $sql) {
        $db->query($sql);
    } #end forreach
} #endif

编辑:我现在有一个很棒的foreach循环,但它需要PDO清理 -

if (($_GET['mode'] == "update") and isset($_GET['id']) and isset($_POST['who'])) {

    $query = "update subcontractors set";
    $comma = " ";

    $whitelist = array("firstname","lastname","address","city","state","zip","phone1","phone2","phone3","email","dob","ssn","website","checks");

    foreach($_POST as $key => $val) {
        if ( !empty($val) && in_array($key, $whitelist)) {
            $query .= $comma . $key . "='" . $val . "'";
            $comma = ", ";
        }
    }
    $query .= " where id=" . $_POST['who'];

    include "connect.php";
    $db->query($query); 
} #endif UPDATE SECTION

0 个答案:

没有答案