Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:17:13+00:00 2026-06-12T06:17:13+00:00

I’m attempting to cut down on the amount of PHP code needed to insert

  • 0

I’m attempting to cut down on the amount of PHP code needed to insert a lot of different $_POST data into a database. I’m currently using PDO with named params and listing them out manually every time. Perhaps there is a cleaner/better way than I’m attempting with my example, however, I’m in a position where I can’t refactor the 100’s of captured variables I already have listed like this:

$digital_fee = (isset ($_POST['digital_fee']) ? $_POST['digital_fee'] : '');    
$banner_1 = (isset ($_POST['banner_1']) ? 1 : 0);

And the remaining PHP – SQL is written out like this:

$statement = $db->prepare("INSERT INTO $table
                            (digital_fee, banner_1)
                            VALUES
                            (:digital_fee, :banner_1)
                            ON DUPLICATE KEY UPDATE
                            digital_fee=:digital_fee, banner_1=:banner_1");

$statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
$statement->bindParam(':digital_fee', $digital_fee);

$statement->execute();

So I’m testing something like the below which I thought to turn into a Class/Function and call it passing in existing assoc arrays e.g. myFunction($db, $table, $arr_digital);

// example function to handle the PDO named params db inserts

$digital_fee ='500';
$banner_1 = '0';

$arr_digital = array("digital_fee"=>$digital_fee, "banner_1"=>$banner_1);   
$table = 'myTable';

//create sql string with $vars
$sql = "INSERT INTO $table (";  
foreach ($arr_digital as $key => $val){
    $sql.= "$key, ";    
}

$sql.= ") VALUES ( ";
foreach ($arr_digital as $key => $val){
    $sql.= ":$key, ";
}

$sql.= ") ON DUPLICATE KEY UPDATE ";    
foreach ($arr_digital as $key => $val){ 
    $sql.= "$key=:$key, ";  
}   
echo $sql;

$statement = $db->prepare($sql);


$statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
foreach ($arr_digital as $key => $val){
         $statement->bindParam(":$key, $val");
}

$statement->execute();

Running this returns an error which I think is format related (hopefully) – here’s the message,
Any pointers gratefully received and sorry this is a long post.

Fatal error:
  Uncaught exception 'PDOException' with message 
    'SQLSTATE[42000]: Syntax error or access violation: 1064 
     You have an error in your SQL syntax; check the manual that corresponds 
     to your MySQL server version for the right syntax to use near 
     ') VALUES ( '500', '0', ) ON DUPLICATE KEY UPDATE digital_fee='500', banner_1='0'' at line 1'

rich.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:17:14+00:00Added an answer on June 12, 2026 at 6:17 am
        $digital_fee ='500';
        $banner_1 = '0';
    
        $arr_digital = array("digital_fee"=>$digital_fee, "banner_1"=>$banner_1);   
        $table = 'myTable';
    
        //create sql string with $vars
        foreach ($arr_digital as $key => $val){
            $columns[]= $key;  
            $vals [] = ':'.$key;
            $updates[]= "$key = :$key";
    
        }
        $column = implode(', ', $columns);
        $values = implode(', ', $vals);
        $update = implode(', ', $updates);
    
        $sql = "INSERT INTO $table ($column) VALUES ($values) ON DUPLICATE KEY UPDATE $update";  
        echo $sql;
    
        $statement = $db->prepare($sql);
    
    
        $statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
        foreach ($arr_digital as $key => $val){
                $statement->bindParam(":$key", $val);
        }
    
        $statement->execute();
    

    Try now .. should do fine not tested

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I've tracked down a weird MySQL problem to the two different ways I was
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.