-3

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);
6
  • 2
    There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed. Commented Dec 31, 2018 at 23:20
  • 1
    It's also open to sql injection; use a prepared statement for this. Commented Dec 31, 2018 at 23:25
  • Doh!! Removed spaces and fixed extra quotes!! Thank you. I will now try and expand my knowledge of PDO prepared statements using the form data and arrays Commented Dec 31, 2018 at 23:45
  • Welcome, cheers Commented Dec 31, 2018 at 23:46
  • Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc. Commented Dec 31, 2018 at 23:58

1 Answer 1

0

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();
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thx Chester!!! Had a quick play. Only showed one value though. Will revisit again tomorrow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.