Beauty akter Beauty akter - 1 year ago 51
SQL Question

Mysql fetch data selection from html checkbox

I want to get multiple return value for checkbox. But problem is with following code check box only can echo a single value. Even I select multiple checkbox I am getting only single value form it. How to solve it

$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");

while ($row = mysqli_fetch_array($q2)) {
$product_img_url = $row['product_img_url'];
$target_dir = "../assets/img/temp_img/";
$img_link = $target_dir.$product_img_url;

echo '<br>Add it<br><input type="checkbox" name="pic_tobe_add" value="'.$img_link.'"><br><img src="'.$img_link.'" class="img-rounded" alt="Uploaded image" width="152" height="118"><br><br>';
if(isset($_POST['submit_p'])) {
if (empty($_POST['pic_tobe_add'])) {
echo "Error: select a pic";
}else{echo $pic_tobe_add = $_POST['pic_tobe_add'];}
echo '<br><input type="submit" name="submit_p" value="Add this product"><form>';

Answer Source

Actually name of each check-box is same that's why you get only single value which is last-one.

change name="pic_tobe_add" to name="pic_tobe_add[]" (Make it array type so that you will get all the value)

On the next page confirm it by printing out POST value using echo "<pre/>";print_r($_POST);