0

I have a page with a dropdown box at the top populated from the database, when I select an item from the dropdown it gives a list of results with a checkbox at the end of each line, I want to be able to select, using each checkbox, any number of results, and submit a value back into a field in the database for each result selected.

I sort of have this working, but it submits the value to every field in the database, rather than just the ones selected

<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico">

<title>Stock Items</title>

<!-- Bootstrap CSS -->    
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- bootstrap theme -->
<link href="css/bootstrap-theme.css" rel="stylesheet">
<!--external css-->
<!-- font icon -->
<link href="css/elegant-icons-style.css" rel="stylesheet" />
<link href="css/font-awesome.min.css" rel="stylesheet" />    
<!-- full calendar css-->
<link href="assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css"     rel="stylesheet" />
<link href="assets/fullcalendar/fullcalendar/fullcalendar.css" rel="stylesheet" />
<!-- easy pie chart-->
<link href="assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/>
<!-- owl carousel -->
<link rel="stylesheet" href="css/owl.carousel.css" type="text/css">
<link href="css/jquery-jvectormap-1.2.2.css" rel="stylesheet">
<!-- Custom styles -->
<link rel="stylesheet" href="css/fullcalendar.css">
<link href="css/widgets.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/style-responsive.css" rel="stylesheet" />
<link href="css/xcharts.min.css" rel=" stylesheet"> 
<link href="css/jquery-ui-1.10.4.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
<!--[if lt IE 9]>
  <script src="js/html5shiv.js"></script>
  <script src="js/respond.min.js"></script>
  <script src="js/lte-ie7.js"></script>
<![endif]-->
</head>

<body>
<!-- container section start -->
<section id="container" class="">

<?php include("navigation.php"); ?>

  <!--main content start-->
  <section id="main-content">
      <section class="wrapper">            
          <!--overview start-->
          <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header"><i class="fa fa-lightbulb-o">    </i>Stock</h3>
                <ol class="breadcrumb">
                    <li><i class="fa fa-home"></i><a href="/">Home</a></li>
                    <li><i class="fa fa-lightbulb-o"></i>Stock</li>                         
                </ol>
            </div>
          </div>

                      <div class="row">
              <div class="col-lg-12">
                  <section class="panel">
                      <header class="panel-heading">
                          All Stock
                      </header>
                      <div class="panel-body">
                      <form class="form-horizontal" method="post" action="">
                      <div class="form-group">
                        <div class="col-lg-8">                      

                                          <select name="search" class="form-control" required >
                                            <?php
                                                 // connect to the database 
                                                require_once('models/db-settings.php');
                                                    $conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn)); 
                                                    mysql_select_db($db_name);

                                                $query = "SELECT `id`, `description` FROM `stock_templates`";
                                                $stock_templates = mysql_query($query);

                                                    echo "<option value=''>Select Stock Template</option>";
                                                while ($description=mysql_fetch_assoc($stock_templates)) {
                                                    echo "<option value='" . $description['id'] . "'>" . $description['description'] . "</option>";
                                                }
                                                ?>
                                          </select>

                        </div>

                        <div class="col-lg-1"> 
                            <input type="submit" name="filter" value="Search" class="btn btn-success" />
                        </div>
                      </div>
                    </form><br><br>                 

                    <?php

if(isset($_POST['formSubmit'])) 
{
    $aDoor = $_POST['check_list'];      

    if(empty($aDoor)) 
    {
        echo("<p>You didn't select any items to add to lease.</p>\n");
    } 
    else 
    {
        $N = count($aDoor);

        echo("<p>You selected $N item(s) to add to lease: ");
        for($i=0; $i < $N; $i++)
        {
            echo($aDoor[$i] . " ");
        }
        echo("</p>");
    }        
}

function IsChecked($chkname,$value)
{
    if(!empty($_POST[$chkname]))
    {
        foreach($_POST[$chkname] as $chkval)
        {
            if($chkval == $value)
            {
                return true;
            }
        }
    }
    return false;
}
?>

<?php

ob_start( );    

if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
       $query = mysql_query("UPDATE stock SET lease_id = $lease_id");
       $result2 = mysql_query($query);

// check if sent    
if ($result2) {
?>
<div class="alert alert-success fade in">
                              <button data-dismiss="alert" class="close close-sm" type="button">
                                  <i class="icon-remove"></i>
                              </button>
                                  <strong>Well done!</strong> Your lease items have been successfully saved.
                          </div> 
<?php
} else {
?>
<div class="alert alert-block alert-danger fade in">
                              <button data-dismiss="alert" class="close close-sm" type="button">
                                  <i class="icon-remove"></i>
                              </button>
                              <strong>Oh snap!</strong> We could not save your lease items.
                          </div> 
<?php
}
}
}       

?>                  
<?php

// connect to the database  
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn)); 
mysql_select_db($db_name);

// Extract filter information

$count = 0;
$search = mysql_escape_string(@$_POST['search']);


// select data from the database
$query2 = "SELECT * FROM `stock_templates` ORDER BY `stock_templates`.`id` DESC LIMIT 0";

// Perform Logic
if (array_key_exists("filter", $_POST)) { 

// query based on search term
$query2 = "SELECT * FROM `stock` WHERE $search=stocktemplate_id AND lease_id=0";
}

$result2 = mysql_query($query2);    

// Result
if (mysql_num_rows($result2) < 1) {
echo "<div align='center'><h2>Please select a stock template above and click search</h2></div>";
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" class="form-horizontal" name="check_list[]">                                          
                                    <div class="form-group">
                                        <label class="control-label col-lg-1" for="lease_id">Lease Number *</label>
                                        <div class="col-lg-10"> 
                                          <select name="lease_id" class="form-control">
                                            <?php
                                                // connect to the database  
                                                require_once('models/db-settings.php');
                                                    $conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn)); 
                                                      mysql_select_db($db_name);

                                                $query = "SELECT `id`, `leasenumber` FROM `lease`";
                                                $leases = mysql_query($query);

                                                    echo "<option value=''>Select Lease..........</option>";
                                                while ($lease=mysql_fetch_assoc($leases)) {
                                                    echo "<option value='" . $lease['id'] . "'>" . $lease['leasenumber'] . "</option>";
                                                }
                                                ?>
                                          </select>
                                        </div>
                                      </div>

                                       <table class="table table-hover">
                          <thead>
                          <tr>
                              <th>Item ID</th>
                              <th>Description</th>
                              <th>Barcode</th>
                              <th>Serial</th>
                              <th>Add To Lease</th>

                          </tr>
                          </thead>
<?php
while ($row = mysql_fetch_array($result2))
{
$id = $row["id"];
$lease_id = $row["lease_id"];
$barcode = $row["barcode"];
$serial = $row["serial"];
$stocktemplate_id = $row["stocktemplate_id"];

$qa = 0;

?>
                          <tbody>
                          <tr>
                              <td><?php print $id ?></td>
                              <td><?php $q = mysql_query("SELECT description FROM stock_templates WHERE id = '$stocktemplate_id'"); while ($row = mysql_fetch_array($q)){$qa = $row["description"];} print $qa ?></td>
                              <td><?php print $barcode ?></td>                        
                              <td><?php print $serial ?></td>
                              <td><input type="checkbox" name="check_list[]" value="<?php print $id ?>" /></td>
                              <td></td>

                 </td>
                          </tr>
                          </tbody>
<?php
}
?>

                      </table>
                       <input class="btn btn-danger" type="submit" name="formSubmit" value="Add Selected To Lease" />
                      </div>
                      </form>
                  </section>
              </div>
          </div>          

      </section>
  </section>
  <!--main content end-->
  </section>
  <!-- container section end -->
  <!-- javascripts -->
   <script src="js/jquery.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <!-- nice scroll -->
    <script src="js/jquery.scrollTo.min.js"></script>
    <script src="js/jquery.nicescroll.js" type="text/javascript"></script>

   <!-- jquery ui -->
   <script src="js/jquery-ui-1.9.2.custom.min.js"></script>

   <!--custom checkbox & radio-->
   <script type="text/javascript" src="js/ga.js"></script>
   <!--custom switch-->
   <script src="js/bootstrap-switch.js"></script>
   <!--custom tagsinput-->
   <script src="js/jquery.tagsinput.js"></script>

   <!-- colorpicker -->

   <!-- bootstrap-wysiwyg -->
   <script src="js/jquery.hotkeys.js"></script>
   <script src="js/bootstrap-wysiwyg.js"></script>
   <script src="js/bootstrap-wysiwyg-custom.js"></script>
   <!-- ck editor -->
   <script type="text/javascript" src="assets/ckeditor/ckeditor.js">    </script>
<!-- custom form component script for this page-->
<script src="js/form-component.js"></script>
<!-- custome script for all page -->
<script src="js/scripts.js"></script>

</body>
</html>
ArK
  • 20,698
  • 67
  • 109
  • 136
JonC
  • 11
  • 3
  • First get rid of mysql_ functions as they are deprecated, second: get rid of mixing logic and mark up. Think about separation of concerns – B001ᛦ Sep 16 '16 at 09:43
  • you echo this $check and update your database using the checkbox value – premi Sep 16 '16 at 09:54
  • Thanks Premi, that was it, I was missing $check, what I am now missing is the value to input into the database, this comes from the dropdown within the form that is selected before submitting but I cant seem to reference the "id" from it? – JonC Sep 16 '16 at 10:21

2 Answers2

0

That is really Easy to give a go.

See this is same as you want.

In this link the first answer by Sean Valsh is your solution.

Giving the array as name of every checkbox will give you only chacked option's id in array while submitted.

https://stackoverflow.com/a/4997271/6834980

Ask if still have the problem. Happy to help.

Community
  • 1
  • 1
  • Thanks for the reply, I had seen that post when I put together the above code and I think I have it the same? That post doesnt show how to insert each line into the database and I think thats the bit I have wrong? Thanks – JonC Sep 16 '16 at 09:57
  • Look at my 2nd answer. You will have it what you're looking for. @JonC – Hardik Trivedi Sep 16 '16 at 10:28
0

No worries. Just put the code of insert query inside foreach() loop. Like this:

foreach($_POST['check_list'] as $item)
{
    $sql="INSERT/UPDATE Query";
    //for example
    $sql = "INSERT INTO table_demo (field_1, TARGET_FIELD, field_2, field_3) VALUES (val_1, $item, val_2, val_3)";
    $insert = mysqli_query($connection,$sql);
}
//next code of your choice