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 6169171
In Process

The Archive Base Latest Questions

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

UPDATE 1: I get the following error: Fatal error: Cannot pass parameter 2 by

  • 0

UPDATE 1:

I get the following error:

Fatal error: Cannot pass parameter 2 by reference in /var/www/page1.php on line 42 Call Stack: 0.0008 341836 1. {main}() /var/www/page1.php:0

When using:

if( $_SERVER['REQUEST_METHOD'] == 'POST') {
    $fields = array(
        'col1'  => 'cb1',
        'col2'  => 'cb2',
        'col3'  => 'cb3',
        'col4'  => 'cb4',
    );
    $parts = array();
    foreach($fields as $dbfield => $field)
        $parts[] = '`' . $dbfield . '` = :' . $dbfield;
    $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(', ', $parts) . ' WHERE `id `= :id');
    // temp simulation value
    $id = 1;
    $sth->bindParam(':id', $id, PDO::PARAM_INT, 4);
    foreach($fields as $dbfield => $field)
        $sth->bindParam(':' . $dbfield, isset($_POST[$field]) ? 1 : 0, PDO::PARAM_INT, 1);
    $sth->execute();
}

ORIGINAL QUESTION:

Will the following code prevent SQL injections?

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        // all the way upto 50
        $fields = array('col1'=>'cb1', 'col2'=>'cb2', 'col3'=>'cb3', 'col4'=>'cb4');

        $update = '';

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

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

            $update .= isset($_POST[$field]) ? 1 : 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" );

        // temp simulation value
        $id = 1;

        $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>
  • 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:48:44+00:00Added an answer on May 23, 2026 at 10:48 pm

    Well, in your particular case, SQL injection cannot go through this code because you’re only constructing your update out of pre-known column names, 1s and 0s. The overall way you’re constructing that update would be a very dangerous practice to repeat in other contexts, though. The thing is, you’re using PDO, but then forcing it to send a raw string with no escaping of your data, which defeats everything PDO can do to try to protect you.

    In order to actually use what PDO does for you against SQL injection, you need to write the query with ? or named placeholders for data and use bindParam to supply the values.

    An example of dynamic construction using binding of named parameters might look like:

    if( $_SERVER['REQUEST_METHOD'] == 'POST') {
        $fields = array(
            'col1'  => 'cb1',
            'col2'  => 'cb2',
            'col3'  => 'cb3',
            'col4'  => 'cb4',
        );
        $parts = array();
        foreach($fields as $dbfield => $field)
            $parts[] = '`' . $dbfield . '` = :' . $dbfield;
        $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(', ', $parts) . ' WHERE `id `= :id');
        // temp simulation value
        $id = 1;
        $sth->bindParam(':id', $id, PDO::PARAM_INT, 4);
        foreach($fields as $dbfield => $field) {
            $value = isset($_POST[$field]) ? 1 : 0;
            $sth->bindParam(':' . $dbfield, $value, PDO::PARAM_INT, 1);
        }
        $sth->execute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can update code correctly, but cannot commit code. I get the following error
I am trying to load a Plugin but I get the following error: Fatal
I'm trying to get the following SQL statement to work: UPDATE myschema.tableA update_tableA SET
I get this error on an update panel within a popupControlExtender which is within
I get a mysql error: #update (ActiveRecord::StatementInvalid) Mysql::Error: #HY000Got error 139 from storage engine:
I get this error when I do an svn update : Working copy XXXXXXXX
When we run a update query we get prompt saying that 'these many records
I continually get these errors when I try to update tables based on another
Normally when you update an object in linq2sql you get the object from a
When doing a cvs update , you get a nice summary of the state

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.