Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
PHP - Multiple bind_params Failing

I'm trying to execute delete SQL statement upon button press, which was working if I binded 1 parameter, but I want to make the delete.php generic, as not to have multiple of them just for referencing different tables:

<?php
include "header.php";
include "db.php";

$_POST['table'] = "customer";
$_POST['column'] = "cID";
$_POST['del_id'] = 26;

if(isset($_POST['del_id']))
{
    if ($stmt = $conn->prepare("DELETE FROM customer WHERE ? = ?"))
    {
        $stmt->bind_param('si', $_POST['column'], $_POST['del_id']);
        $stmt->execute();
    }
    else echo("Oops");
}

This binding executes but doesn't do anything to the table, only binding the final value 'del_id', executes correctly, and binding 3 arguments including the table name, just causes prepare() to fail.

I am setting the _POST vars in other places from AJAX POSTs, above is just for testing that this bit works or not. I also haven't gotten round to doing validation yet before that comes up.

Very PHP nooby, likely a simple mistake, or just something I'm not aware of, in which case I'd be rather curious as to why the table/column names can't be parameterised, as it's been eluding me for some time. As a workaround, would some form of concatenation work instead, to be able to drop dynamic names into this query from multiple different places?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Yes, to achieve this you will need to have a combination of concatenation and parameters for the Prepared Statement:

if ($stmt = $conn->prepare("DELETE FROM " . $_POST['table'] . " WHERE " . $_POST['column'] . " = ?"))
    {
        $stmt->bind_param('si', $_POST['del_id']);
        $stmt->execute();
    }

Make sure you pay attention on additional validations you will need on the table and column names. This should be validated against your data model and not just making sure its a valid identifier. Further, take a look at some ORMs/Query Builders to learn how to elaborate upon this idea. It's a good learning exercise.

about 4 years ago · Santiago Trujillo Report

0

You should validate both the table name and column name before running the delete.

Since you can't prepare either the table or column names, just put them in the sql statement before the prepare.

<?php
    include "header.php";
    include "db.php";

    $_POST['table'] = "customer";
    $_POST['column'] = "cID";
    $_POST['del_id'] = 26;

    //  Add code to prevent SQL injection
    $table = $_POST['table'] == 'customer' ? $_POST['table'] : '';
    $column = $_POST['column'] == 'customer' ? $_POST['column'] : '';

    if(isset($_POST['del_id']) && $table != '' && $column != '') {
        if ($stmt = $conn->prepare("DELETE FROM `".$table."` WHERE `".$column."` = ?"))
        {
            $stmt->bind_param('i', $_POST['del_id']);
            $stmt->execute();
        }
        else echo("Oops");
    }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!