0

i have checked my code 100 times but didn't find any solution for this i have already checked so many websites to find it's solution but didn't find anything. This is an signup system i am trying to make it fully easiest signup system i mean easiest way to use my functions everywhere in my website. I have created a function called database in which i have created a switch statements that will create, connect, update, delete the user data from database by just one function but i am getting an error at Bind_param.

Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables

$query = "INSERT INTO $table (";

foreach ($tableNames as $key => $value) {
    $query .= "$key, ";
}
$query = substr($query, 0, -2);
$query .= ") VALUES (";
$j = 0;
foreach ($tableNames as $key1 => $value2) {
    for ($a=0; $a < $j; $a++) { 
        ++$j;
    }
    $query .= "?, ";
}
$query = substr($query, 0, -2);
$query .= ")"; //correct working
// echo $query;
$stmt = $conn->prepare("$query");

$fieldsNumb = "";
$l = 0;
foreach ($tableNames as $keyss => $valuess) {
    for ($k=0; $k < $l; $k++) { 
        ++$l;                        
    }
    $fieldsNumb .= "s";
}
$fields = "";
foreach ($tableNames as $keys => $values) {
    $fields .= "$"."$keys".",";
}
// echo $fields;

$fields = substr($fields, 0, -2);
// echo $query;
$stmt->bind_param("$fieldsNumb", $fields);

// set parameters and execute
$stmt->execute();
msg("", "success", "Signedup success");

$stmt->close();
$conn->close();
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • `$fields` in `$stmt->bind_param("$fieldsNumb", $fields);` needs to be a set of variables, not a string. IIUC, you should just use `...$tableNames` where you have `$fields` – Nick Oct 02 '22 at 04:10
  • Take a look at [this](https://3v4l.org/SleN0) for an easier way to do this... – Nick Oct 02 '22 at 04:22
  • @Nick Now i am getting this error. `: Uncaught ArgumentCountError: mysqli_stmt::bind_param() does not accept unknown named parameters in ` – Sonny's Kitchen Oct 02 '22 at 12:47
  • @Nick I have checked everything but still didn't find any solution of this too. Please help me. I am not expert in programming. – Sonny's Kitchen Oct 02 '22 at 20:27
  • This is a change in PHP 8... you should be able to fix it with `...array_values($tableNames)` – Nick Oct 02 '22 at 21:47
  • Good to hear. I'm glad I could help – Nick Oct 03 '22 at 07:18

0 Answers0