简体   繁体   中英

Updating multiple MySQL table using arrays with PDO

Very similar to Updating multiple MySQL table columns using arrays with PDO .

But I use a complex form using

<input name="data[Name]" type="text" more stuff>

Updating just gives me a blank page with no errors???

NB: Was using MySQLi and was working fine, just updating to PDO.

$data2 was returning a correct array.

Is this possibly an order thing? IE Output has to match column order?

Have analysed Updating multiple MySQL table columns using arrays with PDO in conjunction with https://guwii.com/bytes/update-multiple-database-columns-using-pdo/

*****UPDATED******

$data = $_POST[data];

$data1 = array(ARRAY INFO HERE);

$data2 = array_merge($data, $data1);

function buildBindedQuery($fields){    
    end($fields);
    $lastField = key($fields);
    $bindString = ' ';
    foreach($fields as $field => $data2){
        $bindString .= $field . '=:' . $field;
        $bindString .= ($field === $lastField ? ' ' : ',');
    }

    return $bindString;                                             
}

$query = 'UPDATE details SET '.buildBindedQuery($data2).' WHERE id='.$_POST['id'].'';
$result = $conn->prepare($query);
$result->execute($data2);

I think that this code can to help you

        $data = $_POST['data'];

        $data1 = array ("Array info here");

        $data2 = array_merge($data, $data1);

        function buildBindedQuery($fields){
        end($fields);
        $lastField = key($fields);
        $bindString = ' ';
        foreach($fields as $field => $data2){
        $bindString .= $field . '=' . $field;
        $bindString .= ($field === $lastField ? ' ' : ',');
         }
         return $bindString;                                                
        }

        $servername = "localhost";
        $username = "";
        $password = '';
        $dbname = "";

        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, 
                        $password);

        $variable = buildBindedQuery($data2);
        $variable2 = explode(",", $variable);

        foreach($variable2 as $var) {
            $va = explode("=", $var);
            $query = "UPDATE usuario SET $va[0]='". $va[1] ."' WHERE id=2";

            $result = $conn->prepare($query);

            $result->execute();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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