douchen7366 2017-02-08 21:40
浏览 40

如果输入复选框,如何更新序列化数组

I am learning about serialization and although I understand the concept I am not familiar with its limitations.

The following is a fictitious scenario, say I have a table called week and inside the table week there are 3 columns. The third column holding serialized data as follows:

INSERT INTO `week` (`week_id`, `meal_code`, `meal_id`) VALUES
(1, 'week12016', 'a:7:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";i:5;s:1:"6";i:6;s:1:"7";}'),

And then I have I have another table called meal and inside meal is as follows:

INSERT INTO `meal` (`meal_id`, `meal_name`) VALUES
(1, 'Chorizo tapas'),
(2, 'Mediterranean halloumi burger'),
(3, 'Lamb Moussaka'),
(4, 'Snappy Asian Fish En Papillote'),
(5, 'Puttanesca Risotto'),
(6, 'Mexican Pork Chilli With Apple Salsa'),
(7, 'Warm Greek Salad');

Now say I have some HTML with some check-boxes as follows:

Monday     [] 
Tuesday    []
Wednesday  []
Thursday   []
Wednesday  []
Thursday   []
Friday     []
Saturday   []
Sunday     []

And then I decide to add another meal with an meal_id of 8 and tick the Monday box, is it possible to update the serialized array to reflect the tick box so it shows as follows:

'a:7:{i:0;s:1:"8";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";i:5;s:1:"6";i:6;s:1:"7";}'),

see how "8" has now gone in place of the "1". The meal table is a little irrelevant since its a serialized array but I have added it for context.

I want to know if its possible to do what I am describing?

Hypothetically I am guessing something like this:

$monday=$_POST['monday'];    
$result = mysqli_query($conn,"SELECT meal_id from WEEK");
$array = unserialize( $result );
$array[0] = $meal_id;
$sfood=serialize($array);

if ($_POST['weekday'] == 'monday')
{
mysqli_query($conn, "UPDATE week SET meal_id ('$sfood') ");                 
} 

I know the above isn't correct but it's what I've tried and have no idea what to go with next.

  • 写回答

1条回答 默认 最新

  • dongmei1988 2017-02-08 22:14
    关注

    The only time you should use serialized data in a database is when it is completely irrelevant to the data relationship... Think storing CSS rules or something, they could be used when you select a row to help style it, but have no bearing on conditions or relationships in the actual data.

    That said, your $result is not actually what you think it is, it's a sql result set... you need to get a row, or rows, out of it:

    while($row = $result->fetchObject()){
      $meal_id = $row->meal_id;
    }
    

    You also need to close the connection when you have what you want, BEFORE running another query:

    $result->close

    But since you seem to just be getting started, you should probably learn about PDO as well. In my experience it's a better fit for a lot of projects.

    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False