Updating multiple MySQL table using arrays with PDO












-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);









share|improve this question




















  • 2





    There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed.

    – Funk Forty Niner
    Dec 31 '18 at 23:20








  • 1





    It's also open to sql injection; use a prepared statement for this.

    – Funk Forty Niner
    Dec 31 '18 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

    – Fraser4655
    Dec 31 '18 at 23:45











  • Welcome, cheers

    – Funk Forty Niner
    Dec 31 '18 at 23:46











  • Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc.

    – cale_b
    Dec 31 '18 at 23:58
















-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);









share|improve this question




















  • 2





    There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed.

    – Funk Forty Niner
    Dec 31 '18 at 23:20








  • 1





    It's also open to sql injection; use a prepared statement for this.

    – Funk Forty Niner
    Dec 31 '18 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

    – Fraser4655
    Dec 31 '18 at 23:45











  • Welcome, cheers

    – Funk Forty Niner
    Dec 31 '18 at 23:46











  • Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc.

    – cale_b
    Dec 31 '18 at 23:58














-3












-3








-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);









share|improve this question
















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);






php post pdo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 23:59









cale_b

19.8k44882




19.8k44882










asked Dec 31 '18 at 23:18









Fraser4655Fraser4655

165




165








  • 2





    There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed.

    – Funk Forty Niner
    Dec 31 '18 at 23:20








  • 1





    It's also open to sql injection; use a prepared statement for this.

    – Funk Forty Niner
    Dec 31 '18 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

    – Fraser4655
    Dec 31 '18 at 23:45











  • Welcome, cheers

    – Funk Forty Niner
    Dec 31 '18 at 23:46











  • Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc.

    – cale_b
    Dec 31 '18 at 23:58














  • 2





    There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed.

    – Funk Forty Niner
    Dec 31 '18 at 23:20








  • 1





    It's also open to sql injection; use a prepared statement for this.

    – Funk Forty Niner
    Dec 31 '18 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

    – Fraser4655
    Dec 31 '18 at 23:45











  • Welcome, cheers

    – Funk Forty Niner
    Dec 31 '18 at 23:46











  • Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc.

    – cale_b
    Dec 31 '18 at 23:58








2




2





There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed.

– Funk Forty Niner
Dec 31 '18 at 23:20







There are extra spaces that should be removed; they do count you know. Plus you have extra quotes that need to be removed.

– Funk Forty Niner
Dec 31 '18 at 23:20






1




1





It's also open to sql injection; use a prepared statement for this.

– Funk Forty Niner
Dec 31 '18 at 23:25





It's also open to sql injection; use a prepared statement for this.

– Funk Forty Niner
Dec 31 '18 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

– Fraser4655
Dec 31 '18 at 23:45





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

– Fraser4655
Dec 31 '18 at 23:45













Welcome, cheers

– Funk Forty Niner
Dec 31 '18 at 23:46





Welcome, cheers

– Funk Forty Niner
Dec 31 '18 at 23:46













Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc.

– cale_b
Dec 31 '18 at 23:58





Also, code formatting helps you a lot. I'd recommend formatting your code with proper indenting, etc.

– cale_b
Dec 31 '18 at 23:58












1 Answer
1






active

oldest

votes


















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();
}





share|improve this answer
























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

    – Fraser4655
    Jan 2 at 7:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992085%2fupdating-multiple-mysql-table-using-arrays-with-pdo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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();
}





share|improve this answer
























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

    – Fraser4655
    Jan 2 at 7:37
















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();
}





share|improve this answer
























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

    – Fraser4655
    Jan 2 at 7:37














0












0








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();
}





share|improve this answer













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();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 2:10









Chester mi niñoChester mi niño

3115




3115













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

    – Fraser4655
    Jan 2 at 7:37



















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

    – Fraser4655
    Jan 2 at 7:37

















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

– Fraser4655
Jan 2 at 7:37





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

– Fraser4655
Jan 2 at 7:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992085%2fupdating-multiple-mysql-table-using-arrays-with-pdo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas