0

Let's say I have an array of strings:

$arr = array('prop1', 'prop2', 'prop3');

and I want to use this array to create bind_param for sql request. Each string of the array however should be inside $_POST[]. So this should be the result:

$stmt->bind_param("sss", $_POST['prop1'], $_POST['prop2'], $_POST['prop3']);

Is something like that possible (PHP 7.x)?

11
  • 3
    No, that is not directly possible. But you can create your own array that contains the three elements under those three keys first, and then use the spread operator in your bind_param call. Commented Sep 7, 2022 at 12:12
  • @CBroe the array is used in another places like MySQL UPDATE SET values and then in form fields. So it can't be made by $_POSTs Commented Sep 7, 2022 at 12:22
  • 1
    Once you get the keys you need from $_POST you can just unpack the array in bind_param using ... (splat operator) Commented Sep 7, 2022 at 12:23
  • "So it can't be made by $_POSTs" - I don't get what you are trying to say there. You said you wanted to use $_POST['prop1'] in your bind_param statement, so where else is that value supposed to come from, if not $_POST? Commented Sep 7, 2022 at 12:24
  • 2
    I meant create a new array here specifically for this. For each parameter name you have in $arr, you add the corresponding $_POST element to your new array. And then you use that new array in your bind_param call, with the spread operator. Commented Sep 7, 2022 at 12:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.