简体   繁体   English

从复选框中获取多个值并存储在DB php中

[英]getting multiple values from checkboxes and storing in DB php

Having trouble storing multiple checkbox values into DB. 无法将多个复选框值存储到DB中。 Not sure if this is the most efficient way but it's the only thing I could come up with. 不确定这是否是最有效的方式,但这是我唯一能想到的。 If there are better ways to do it please share. 如果有更好的方法,请分享。 I am still in test mode so there is no validation yet. 我还处于测试模式,所以还没有验证。

ITEMS COMING FROM DB 来自DB的项目

 $get_products = $db->prepare("select * from item where user_id = '$id' ORDER BY add_date");

 $get_products->execute();

 while ($row = $get_products->fetch())
 {
   $item_id =  $row['item_id'];
   $user_id =  $row['user_id'];
   $item_name = $row['item_name'];

   $products .= "<br/><input type='checkbox' name='items[]' value='$item_id' />$item_name";
 }

FORM 形成

<form method="post" action="confirm.php">

    <?php echo $products; ?>

    <input type="submit" value="Add Items" id="add_items"/>

    <input name="from_id" type="hidden" value="1">
    <input name="to_id" type="hidden" value="2">
    <input type="submit" name="submit" value="Submit" >
 </form>

PROCESS 处理

if(isset($_POST['submit']))
{
         $from = $_POST['from_id'];
     $to = $_POST['to_id'];
     $items = $_POST['items'];

   if(empty($items))
   {
      $message = "no items in items";
      exit;
   }

        foreach($items as $i)
    {
          $items .= $i. "|";
    }
         json_encode($items);

$sql = $db->prepare("INSERT into trans(from_id, to_id,items)VALUES(:from_id, 
                         :to_id, :items");

$sql->bindValue('from_id', $from);
$sql->bindValue('to_id', $to);
$sql->bindValue('items', $items);

if($sql->execute())
{
    header("Location: profile.php?user=1");
    exit();
    }

The first issue I see is that you are overwriting your $items variable with a blank string when you do this: 我看到的第一个问题是,当你这样做时,你用一个空字符串覆盖你的$items变量:

$items = $_POST['items'];

$items = "";

This means you will be running a foreach on a blank string. 这意味着您将在空字符串上运行foreach

Although a better way to store the values from your checkboxes would be to just json_encode() the original $items variable. 虽然从复选框中存储值的更好方法是json_encode()原始的$items变量。 This will encode the value you receive from your form into a JSON string which can safely be stored in a database. 这会将您从表单接收的值编码为JSON字符串,该字符串可以安全地存储在数据库中。

Then when you want to get the data back out of the database, simply run json_decode() on the JSON string and the JSON string will be converted back to an array. 然后,当您想要从数据库中取回数据时,只需在JSON字符串上运行json_decode() ,JSON字符串将转换回数组。

Though, if you want an associative array to be returned from json_decode() , be sure to pass true into the second parameter like so: 但是,如果你想从json_decode()返回一个关联数组,请确保将true传递给第二个参数,如下所示:

$indexed_array = json_decode($some_array);
$associative_array = json_decode($some_array, true);

EDIT 编辑

Providing the data is being passed from the form, this is what you will need in your confirm.php file: 提供数据正在从表单传递,这是您在confirm.php文件中需要的:

if(isset($_POST['submit']))
{
  $from = $_POST['from_id'];
  $to = $_POST['to_id'];
  $items = $_POST['items'];

  if(empty($items))
  {
    $message = "no items in items";
    exit;
  }

  $items = json_encode($items);

  $sql = $db->prepare("INSERT into trans(from_id, to_id,items)VALUES(:from_id, 
                     :to_id, :items");

  $sql->bindValue('from_id', $from);
  $sql->bindValue('to_id', $to);
  $sql->bindValue('items', $items);

  if($sql->execute())
  {
    header("Location: profile.php?user=1");
    exit();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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