简体   繁体   中英

If value match then checkbox checked and update database with checkbox values on submit

I'm trying if value is in database show checkbox checked. I checked different value and click on submit button that update database where update value is checkbox checked value.

Here is my code:

    <?php
    include "database_connection.php"; 
    $cutomername = $_GET['username'];
    $productid = $_GET['userid'];
    $cust_id = $_GET['custid'];
    $productid_arr = explode(',' , $productid);
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> <?php echo $cutomername;?></title>
    </head>

    <body>

    <form method="post" name="checkboxForm">
    <?php

    $query_flags = "select * from products";
    $row_flags=mysql_query($query_flags); 
    $i=0;

    while ($row = mysql_fetch_array($row_flags)){

           $product_id = $row['product_id'];
           $product= $row['product'];
           if($productid_arr[$i] ==$row['product_id']) 
           {
              $check = 'checked="checked"';
           } else{
              $check = '';
           }
           if(in_array($row['product_id'], $chvalues))
           {
              echo  "<input   type=\"checkbox\" name=\"checkbox[]\"  id=\"$product_id\" value='" . $row['product_id'] . "' $check />";
           } else{
              echo  "<input  type=\"checkbox\" name=\"checkbox[]\"  id=\"$product_id\" value='" . $row['product_id'] . "' $check/>";
           }
    echo $row['product'];
    echo "<hr />"; 
    $i++;
    }   
    ?>  
    <input class="submit" type="submit" value="Submit" name="submit">   

    <?php
    $chvalues = array();
    if(isset($_POST['checkbox']))
    {
       foreach($_POST['checkbox'] as $ch => $value)
       {
       $chvalues[] = $value;
       }
    }

    $des_prod_id = implode(',' , $chvalues);
    $query = mysql_query("UPDATE customer SET product_id = '$des_prod_id'  where  ID = '$cust_id'");
   ?>

</form>
</body>
</html>

Change this line :

 $query = mysql_query("UPDATE customer SET product_id = $products where  customer_name = $cutomername");

to:

$query = mysql_query("UPDATE customer SET product_id = $value where  customer_name = $cutomername");

$products should be $value because you iterate and assign each element to this variable in your foreach loop.

finally i solved this problem

here is my code:

<?php
    include "database_connection.php"; 
    $cutomername = $_GET['username'];
    $productid = $_GET['userid'];
    $cust_id = $_GET['custid'];
    $productid_arr = explode(',' , $productid);


 if(isset($_POST['submit']))
     {
     $productid_arr = array();
     foreach($_POST['checkbox'] as $ch => $value)
     {
        $productid_arr[] = $value;
     }

    $des_prod_id = implode(',' , $productid_arr);

    $query = mysql_query("UPDATE customer SET product_id = '$des_prod_id'  where  ID = '$cust_id'");
    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> <?php echo $cutomername;?></title>
    </head>

    <body>

    <form method="post" name="checkboxForm">
    <?php

    $query_flags = "select * from products";
    $row_flags=mysql_query($query_flags); 
    $i=0;

    while ($row = mysql_fetch_array($row_flags)){

           $product_id = $row['product_id'];
           $product= $row['product'];
           if(in_array($product_id, $productid_arr)) 
           {
            $check = 'checked';
            } else{
            $check = '';
            }
        echo  "<input   type=\"checkbox\" name=\"checkbox[]\"  id=\"$product_id\" value='" . $row['product_id'] . "' $check />";
        echo $row['product'];
    echo "<hr />"; 
    $i++;
    }   
    ?>  
    <input class="submit" type="submit" value="Submit" name="submit">   

</form>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM