title: Dynamic prepared statement, PHP
body: <p>I've checked almost all questions that produce the same error but all of these questions bind parameters in some wrong way. Perhaps and most probably I too am binding params incorrectly though my case is different because I've dynamic query.</p>
  
  <p>I am creating query dynamically from input which is being created perfectly. But problem comes from <code>$stmt-&gt;bind_param</code> statement within <code>foreach</code> loop. Here is my Code snippet that is erronous:</p>
  
  <pre><code>$query = "UPDATE users SET";
          foreach($updationFields as $field =&gt; $value){
              if($value != "-"){
                  $query = $query. " " . $field . " = :".$field.",";
              }
          }
          $query = rtrim($query, ",");
          $query = $query . " WHERE UserId = :UserId";
  
          $stmt = $this-&gt;conn-&gt;prepare($query);
          foreach($updationFields as $field =&gt; $value){
              echo $field;
              if($value != "-"){
                  $input = ":".$field;
                  $stmt-&gt;bind_param($input, $value); // This line produces error
              }
          }
          $stmt-&gt;bind_param(":UserId", $userId);
  
          $stmt-&gt;execute();      
  </code></pre>
  
  <p>Here is produced dynamic "string query" for one field:</p>
  
  <pre><code>UPDATE users SET fullName = :fullName WHERE UserId = :UserId
  </code></pre>
  
  <p>Error says: <strong>Fatal error: Call to a member function bind_param() on a non-object in</strong></p>
  
  <p>Any Idea what i am doing wrong?</p>