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

  • Home
  • SEARCH
  • 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 6166795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:23:58+00:00 2026-05-23T22:23:58+00:00

I have the following code which works, but it’s not using PDO which is

  • 0

I have the following code which works, but it’s not using PDO which is what I normally use:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

        // will go all the way upto 50
        $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4');

        $update = '';

        foreach($fields as $dbfield => $field) {
            if ($update) $update.= ',';

            $update.= ' '.$dbfield.'=';

            if (isset($_POST[$field])) {
                $update.= 1;
            } else {
                $update.= 0;
            }
        }

        // show generated query
        echo 'UDPATE table SET'.$update.' WHERE 1=1';
    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb1" />
            <input type="checkbox" name="cb2" />
            <input type="checkbox" name="cb3" />
            <input type="checkbox" name="cb4" />
            <!-- all the way to 50 -->

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

Because I am getting the column names AND the column values to be updated from the code above, I am not sure how to do this using PDO?

I have done the following code below:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

        $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4');

        $update = '';

        foreach($fields as $dbfield => $field) {
            if ($update) $update.= ',';

            $update.= ' '.$dbfield.'=';

            if (isset($_POST[$field])) {
                $update.= 1;
            } else {
                $update.= 0;
            }
        }

        $DBH = new PDO( "mysql:host=localhost;dbname=database", "user", "pass" );
        $DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $STH = $DBH -> prepare( "update table1 set :update where id = :id" );

        $STH -> bindParam( ':update', $update, PDO::PARAM_STR, 255 );
        $STH -> bindParam( ':id', $id, PDO::PARAM_INT, 4 );

        $STH -> execute();
    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb1" />
            <input type="checkbox" name="cb2" />
            <input type="checkbox" name="cb3" />
            <input type="checkbox" name="cb4" />
            <!-- all the way to 50 -->

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

But it gives me the following error:

[Tue Jul 19 09:15:44 2011] [error] [client ::1] PHP 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 ” db_field1=0, dbfield2=1,
dbfield3=1, dbfield4=0′ where id = 1′ at line 1’ in
/var/www/page1.php:30\nStack trace:\n#0
/var/www/page1.php(30): PDOStatement->execute()\n#1
{main}\n thrown in /var/www/page1.php on line 30,
referer: http:// localhost/page1.php

  • 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-05-23T22:23:59+00:00Added an answer on May 23, 2026 at 10:23 pm

    Use:

    $DBH = new PDO( "mysql:host=localhost;dbname=database", "user", "pass" );
    $DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
    $STH = $DBH -> prepare( "update table1 set " . $update . " where id = :id" );
    
    //$STH -> bindParam( ':update', $update, PDO::PARAM_STR, 255 );
    $STH -> bindParam( ':id', $id, PDO::PARAM_INT, 4 );
    
    $STH -> execute();
    

    instead of:

    $DBH = new PDO( "mysql:host=localhost;dbname=database", "user", "pass" );
    $DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
    $STH = $DBH -> prepare( "update table1 set :update where id = :id" );
    
    $STH -> bindParam( ':update', $update, PDO::PARAM_STR, 255 );
    $STH -> bindParam( ':id', $id, PDO::PARAM_INT, 4 );
    
    $STH -> execute();
    

    Or you can use this:

    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { 
        $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4');
    
        $update = array();
        $values = array();
    
        foreach($fields as $dbfield => $field) {
            $update[] = $dbfield . " = ? ";
            $values[] = isset($_POST[$field]) ? 1 : 0;
        }
    
        $values[] = $id;
    
        $DBH = new PDO( "mysql:host=localhost;dbname=database", "user", "pass" );
        $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
        $STH = $DBH->prepare( "update table1 set " . join( ',', $update ) . " where id = ?" );
    
        $STH->execute($values);
    }
    

    instead of:

    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { 
        $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4');
    
        $update = '';
    
        foreach($fields as $dbfield => $field) {
            if ($update) $update.= ',';
    
            $update.= ' '.$dbfield.'=';
    
            if (isset($_POST[$field])) {
                $update.= 1;
            } else {
                $update.= 0;
            }
        }
    
        $DBH = new PDO( "mysql:host=localhost;dbname=database", "user", "pass" );
        $DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
        $STH = $DBH -> prepare( "update table1 set :update where id = :id" );
    
        $STH -> bindParam( ':update', $update, PDO::PARAM_STR, 255 );
        $STH -> bindParam( ':id', $id, PDO::PARAM_INT, 4 );
    
        $STH -> execute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works fine in notepad but not in WORD!!
I have the following code, which works fine on live site, but not on
I have following code which works fine in firefox and ie 8, but in
I have the following code which works perfectly, but I want to allow access
I have the following code which works as expected: #include <iostream> using namespace std;
I have the following line of PHP code which works great: exec( 'wget http://www.mydomain.com/u1.php
I have following code which works fine in iPhone application but in iPad I
I have the following code which works well, but the problem is that after
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return

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.