foreach - What is the proper way in PHP multiple post values as input for MySQL query? -
i make code data post, post contains checkbox selections (multiple selections), , feed these data mysql select.
my basic code is:
echo "<form action='handler.php' method='post'>"; echo '<input type="checkbox" name="cbtest" value="10">href="details.php?id=10">data 1</a>'; echo '<input type="checkbox" name="cbtest" value="11">href="details.php?id=11">data 2</a>'; echo '<input type="checkbox" name="cbtest" value="12">href="details.php?id=12">data 3</a>'; echo "<input type='submit' name='button' value='some action'>"; echo '</form>';
handler.php contains:
$temp = $_post['cbtest']; if(isset($_post['cbtest'])) { foreach ($temp $cbtest){ echo $cbtest."<br>"; }
it clear $cbtest variable contain actual posted data each round of foreach command running.
but how can catch data $cbtest , run query statement this:
$query = "select data_id, data_content data_id = $cbtest";
i display data_content table data each matching id/value in post variable.
how needed write correctly ?
if want multiple values checkbox selection make change follow
<input type="checkbox" name="cbtest[]" value="10">href="details.php?id=10">data 1</a>
now in php $_post['cbtest'] return array of checked inputs. here php code need manipulate query.
$checkedinputs = implode(',',$_post['cbtest']); $query = "select data_id, data_content data_id in (".$checkedinputs.")";
ps: please escape inputs , change query prepeared statement.
Comments
Post a Comment