php - Use one bind_param() with variable number of input vars -


I try to use variable binding in this way:

  $ stmt = $ Mysqli-> and; ("Mytable SET myvar1 =?, Myvar2 = ... WHERE id =?") {$ Stmt-> Bind_param ("ss ...", $ _POST ['myvar1'], $ _POST ['myvar2'] ...);  

But some $ _POST ['...'] may be empty so I do not want to update them in DB.

This is not practical, keeping in mind all the different combinations of empty $ _POST ['...'] and although I can create my updates "Update mytable SET ..." , Bind_param () is a different animal.

I can try to make my call as a string and use eval on it, but it does not look right: (

You can use the function to call the bind_param method with a variable number or argument:

< ($ Name = ',' myvar2 ', / * ... * /); $ params = array (); Foreign currency ($ names as $ names) {if (isset ($ _ POST [$ name]) & amp; $ _POST [$ name]! = '') {$ Params [$ name] = $ _POST [$ name];}} If (counting ($ params)) {$ Query = 'A Date mytable SET '; foreach ($ params $ name = & gt; $ Val) {$ query. = $ Name.' =?, ';} $ Query = substr ($ query, 0, -1); $ qu Ery . $ = '$ Mysqli- & gt; Ready ($ query); $ Params = array_merge (array (str_repeat (' s', counters ($ params)), array_values ​​($ Params); call_user_func_array (array (& amp; $ stmt, 'bind_param'), $ params);}

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -