jeudi 14 avril 2016

Foreach is picking the first checkbox only if checked

I am working on the following code in order to pick checkboxes from a form. If i check the first checkbox everything works great. If i check another checkbox i get the "Undefined index" error. Keep in mind that i am getting the checkboxes with post method and the submit button is above the checkboxes due to the complexity of the location of the form and the fields. What i need essentially is to pick multiple checkboxes and add certain values to the database.

 <?php
session_start();
  if($_SESSION['admin_logged_in'] != true){
    header("Location:login.html");
    exit();
  }
  include 'db.php';

  $from = mysql_real_escape_string($_GET['from']);
  $room = mysql_real_escape_string($_POST['room']);

if(!empty($_POST['id'])) {
    foreach($_POST['id'] as $check) {
      $id = $check;

      $sel = mysql_query("select * from $from where id = '$id' limit 1 ") or die(mysql_error());

      while($row = mysql_fetch_array($sel)){
        $preview = $row['preview'];
        $text = $row['text'];
        $title = $row['title'];
        $images = $row['images'];
  }

      $ins = mysql_query("insert into $room (id, preview, text, title, images) values (' ', '$preview', '$text', '$title', '$images') ") or die(mysql_error());



    }

    header("Location:admin.php");
}

?>

Any help would be greatly appreciated. Thanks.




Aucun commentaire:

Enregistrer un commentaire