Multi-dimensional array into DB table

I have a multi-dimensional array in which it’s inner arrays vary in length(linkl) …line 13-15.

The inner arrays are suppose to update rows in a table.
The SQL query in line 35 tries to do that…

I am trying though to make a prepared statement for that…and I cannot construct the code to cater for the fact that the inner arrays vary in length…currently I am stuck in the bind_param expression…this is where I am:

   foreach ($servicedta as $update) 
                  {
                   $update = array_intersect_key($update, $allowed);
                            $id = array_pop($update);//το update κρατάει τα values προς updating και το allowed το maximum κάθε φορά
                            $set = [];
                            foreach ($update as $field => $value) {
                                $set[] = "`{$field}`=:{$value}";
                            }
                             
            if($stmt=$connection->prepare('UPDATE services_list SET"' . implode(', ', $set). '" WHERE serviceID="'.$id))
                              {
                             
 $stmt->bind_param(str_repeat("s", count($set)+1),$service,$price,$price_show,$ID);//problem here
                              }
   }

the problem lies in the variable definition…the number of it must vary accordingly.
within the foreach loop.

Any ideas?

It looks like you’re using mysqli but that doesn’t support named parameters.

I do not quite understand what that means and how it is related to my problem.
Can you provide an example of named parameters…

The only thing I assume it is the fact that it is related to bind_param() somehow.

Yes, in this bit of code

 $set[] = "`{$field}`=:{$value}";

you’re using a parameter in the form of :name, which is a PDO feature. mysqli parameters are just ? in the query string.

This part is a bit strange too, it will end up with your entire SET list enclosed in double-quotes

  if($stmt=$connection->prepare('UPDATE services_list SET"'  .. '" WHERE serviceID="'.$id))

Is that valid? I don’t recall seeing it done in the samples I’ve looked at.

anyway…I need to ask one more question very much related to to what we have discussed so far.
Take a look at this link line 22.

Can bind_param() in MySQLi accept in it;s second part a splat operator with an array?

Short answer: yes
Long answer: https://phpdelusions.net/pdo/mysqli_comparison#in

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.