I have a dilemma. Maybe it's an easy fix but i just cant see the solution.
My code already works without preparing the sql. The problem is preparing the sql and building the dynamic query. In a nut shell, I have a form with 10 different fields. And depending on which field gets data, I build my query so that it only works on the fields the user typed something into.
let me summarize ....
I have a basic query that starts off like this
$updatestring = "";
$query = "UPDATE table a SET ";
if field1 set then $updatestring = $updatestring . " field1 = field1val "
if field2 set then $updatestring = $updatestring . " field2 = field1val "
if field3 set then $updatestring = $updatestring . " field3 = field1val "
if field4 set then $updatestring = $updatestring . " field4 = field1val "
and so on ....
Finally $query = $query . $updatestring which can end up looking like:
UPDATE table a SET field1 = field1val, field3 = field1val .... depending on which fields were set.
I take care of commas and so on. Like I said this works.
However, to prepare the statement really complicates things because if it looks like this:
UPDATE table a SET field1 = ? , field3 = ?
how do I know which variable to bind to the param out of the 10 possible fields ? How do I build the string types and place them in the right order? Do you see my dilemma?
mysqli_stmt_bind_param($stmt, 'ii', $field1val, $field3val);
Any help would be appreciated. I will also post this in the mysql section.
Thanks,
JT