douwei1904 2013-10-29 23:59
浏览 35

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

Having trouble storing multiple checkbox values into 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

 $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();
    }
  • 写回答

1条回答 默认 最新

  • douhao2721 2013-10-30 00:07
    关注

    The first issue I see is that you are overwriting your $items variable with a blank string when you do this:

    $items = $_POST['items'];
    
    $items = "";
    

    This means you will be running a foreach on a blank string.

    Although a better way to store the values from your checkboxes would be to just json_encode() the original $items variable. This will encode the value you receive from your form into a JSON string which can safely be stored in a database.

    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.

    Though, if you want an associative array to be returned from json_decode(), be sure to pass true into the second parameter like so:

    $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:

    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();
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了