mardi 20 janvier 2015

Insert value of checkbox and number input into database

I tried combining two answers from here: counting how many checkbox are checked php/html And here: validating input types "checkbox" and "number" php/html


My goal is to count how many checkboxes were selected, and insert their values along with the number value in the number input.


I found various other posts about it, but couldn't figure out an answer.


Here is the code:



echo '<form action="" method="post">';
echo '<tr>';
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[.$id.][id]' value='$id'></td>";
echo "<td style='vertical-align: top;'><input type='number' name='choice[$id][order]' size='20'></td>";
echo '</tr>';
echo '<b> <input type="submit" value="Insert"></b>';


Then, on the next page, I have this:



$var_checkbox=$_POST['choice'];
$sql_var_id = "SELECT id FROM custom_form WHERE id=(SELECT max(id) FROM custom_form)";
$var_id_result = mysqli_query($link,$sql_var_id);
$var_id = mysqli_fetch_array($var_id_result);

$count=count($var_checkbox[$var_id[0]]);

for($i=0; $i<$count; $i++){
if($var_checkbox[$i]!= NULL){

$sql1 = sprintf("INSERT INTO custom_form_has_property (custom_form_id,property_id,field_order) VALUES ('%d','%d','%d');",
mysql_real_escape_string($var_id[0]),
mysql_real_escape_string($var_checkbox[$var_id[0]][id]),
mysql_real_escape_string($var_checkbox[$var_id[0]][order])
);
$result1 = mysqli_query($link,$sql1);
}
}


The problem is, that no values of checkboxes or numbers are inserted.





Aucun commentaire:

Enregistrer un commentaire