logo
down
shadow

PDO: defining parameters using foreach and $_POST


PDO: defining parameters using foreach and $_POST

By : Henry Osim
Date : November 19 2020, 12:41 AM
To fix the issue you can do I have simplified the code to illustrate how to create dynamic UPDATE query. This uses "lazy" binding and regular placeholders ?.
The text inputs are set as a regular array (ie. not associative) of values.
code :
<?php
if(isset($_POST['submit'])){
    if (isset($_POST["info"]) && !empty($_POST["user_id"])) {
        $query = "UPDATE `users` SET ";
        $query_params = array();
        $i =1;
        foreach ($_POST['info'] as $value) {
            // push array with each valid info entry ...
            if ($value) {
                $query .= "`info".$i. "` = ?,"; 
                array_push($query_params,$value);
            }
            $i++;
        }
        $query = rtrim($query, ',');//remove trailing ,
        //  
        $query .= " WHERE `id` = ?"; 
        array_push($query_params,$_POST['user_id']);
        //Show query and parameter array Remove after testing
        echo $query;    
        echo "<br>";
        print_r($query_params);
        // Execute the query
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
}
?>
<form method="POST" action="#">
<input type="text" name="info[]" id="info" value=""> 
<input type="text" name="info[]" id="info"  value="">
<input type="text" name="info[]" id="info"  value="">
<input type="text" name="info[]" id="info" value="">
<input type="text" name="user_id" id="user_id" value="">
<input type="submit" name="submit" value="Submit">
UPDATE `users` SET `info1` = ?,`info2` = ? WHERE `id` = ?
Array ( [0] => john [1] => smith [2] => johnsmith ) 


Share : facebook icon twitter icon
foreach $_POST.. is this bad?

foreach $_POST.. is this bad?


By : Akanksha Sharma
Date : March 29 2020, 07:55 AM
help you fix your problem There is no great harm in automatically processing all of POSTed fields, as long as that processing is limited to storing in a hash. Matter of fact, it can make fore a significantly easier to maintain code.
The moment you start doing something (especially system related) with that data, you need to validate.
Form action with parameters - parameters are not in $_GET or $_POST

Form action with parameters - parameters are not in $_GET or $_POST


By : AndreaMarinetti
Date : March 29 2020, 07:55 AM
it fixes the issue $_GET and $_POST are empty because you are using the POST protocol, but sending variables through the GET method (encoding them in the URL).
PHP is finding the POST method used, so it sets the information in the $_GET variable to nothing, then parses the variables sent in the form's POST data into the $_POST variable, ie: nothing.
foreach ($_POST)

foreach ($_POST)


By : Jrnr601
Date : March 29 2020, 07:55 AM
I wish did fix the issue. In a
Foreach loop that loops through $_POST data instead of static isset($_POST["name"]......)

Foreach loop that loops through $_POST data instead of static isset($_POST["name"]......)


By : Sreenu
Date : March 29 2020, 07:55 AM
it helps some times So here I am again trying to find better ways of doing things. 90% of tutorials do things the normal way below: , try this:
code :
$check=true;   
if(isset($_POST)){
  foreach($_POST as $key=>$value){
   if(!isset($_POST[$key]){
    $check = false;
    break;
     } 
  }
}
$keys =array("input1","input2");

$check=true;   
if(isset($_POST)){
  foreach($keys as $input){
   if(!array_key_exists($input,$_POST)){
    $check = false;
    break;
     } 
  }
}
I want to write a function that uses $_POST['first_name '] and $_POST['password '] as parameters

I want to write a function that uses $_POST['first_name '] and $_POST['password '] as parameters


By : Paul Tan
Date : March 29 2020, 07:55 AM
wish helps you As already stated by commenters, your second code is the right one.
But it can be simplified:
Related Posts Related Posts :
  • Curl PHP cannot display amazon
  • Symfony, getters and setters vs magic methods
  • Using 'continue' PHP instruction outside the loop
  • AJAX POST return data not appearing
  • Can I query relations using an INNER JOIN instead of two queries in Eloquent?
  • Looping through dynamic form fields and inserting into database
  • My php code can't select mysql auto_increment value
  • Store Angularjs form data in database using php
  • I want to run my sh file continuously even if I close my Putty connection
  • file_get_contents equivalent for gzipped files
  • Include PHP file with jQuery
  • php curl headers do not return from website?
  • How to find out, if facebook ID is a user, group or page
  • Connect webhost database to android database
  • preg_match get div content with class
  • Upload multiple files in Laravel 4
  • Count array numbers in multidimensional array
  • PHP Date diff with a difference
  • Search Customer by custom field in Netsuite
  • Is it possible to hide/encode/encrypt php source code and let others have the system?
  • list items to be displayed using php code and array
  • check if row exists mysqli
  • PHP errors loading MySQL
  • setup PostgreSQL with Laravel in MAMP
  • PHP - CodeIgniter Notifications
  • Encrypting a password column in a SQL database
  • New to PHP, trying to extract information from another website
  • JavaScript AmChart to Image for Email
  • Is javascript validation enough to keep my forms secure?
  • Regex extract variables from [shortcode]
  • Download a .mp3 link and save it to computer with PHP
  • Error with the ereg_replace
  • Getting the fields attached to a bundle of an entity in Drupal
  • Php how to check multiple times (within `foreach`) if values from array exists in mysql?
  • MySQL and PHP parsing strange string
  • Pull specific data from multidimensional associative array
  • How to get the original URI extension using PHP Tonic?
  • Upload videos and images from Android to PHP server?
  • Unset a multidimensional array with another multidimensional array with values than key
  • Calling a user e-mail to add to a mailto link PHP
  • Why is override_function coming back as an undefined function
  • PHP mySQLi_fetch_all: iterate through each row
  • Using pow in php
  • What's wrong with my PHP Script? T_String issue
  • how to call a function of another file without including that file
  • is_rgb function in php
  • transform the numbers to letters using php
  • How to catch an exception from another class method PHP
  • PHP exec is blocked by ISP
  • PHP4 to PHP5 migration character encoding. latin1 vs utf8
  • Begin Transaction not defined
  • How much harm can a user do with xss on a page unique to them?
  • How to import data from a php file to javascript variable
  • PHP and Javascript - log complete workflow
  • When I echo !!! 0 it gives me a strange result
  • jquery ajax post dies after submit
  • How to upload file with curl on sftp server
  • Advanced search using a text input to search through only one category
  • Why do I get "Non-static method App\Models\Category::products() should not be called statically" in laravel
  • Stop notices from undefined indexes in arrays
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org