简体   繁体   English

serialize()和unserialize()的问题-插入和选择数据PHP MySQL

[英]Problems with serialize() and unserialize() - inserting and selecting data PHP MySQL

I am attempting to grab a date supplied via POST, then generate a list of dates over a 12 week period from the supplied start date. 我试图获取通过POST提供的日期,然后从提供的开始日期起12周内生成日期列表。 These dates would then go into the DB and a 12 week schedule would be output, which the user can interact with (add/edit/delete). 然后将这些日期输入数据库,并输出12周的时间表,用户可以与之交互(添加/编辑/删除)。

I am successfully taking the start date, generating the 12 week date list and adding this into the DB in serialized form, but when it comes to selecting the dates for display, I get the following error: 我成功地获取了开始日期,生成了12周的日期列表并将其以序列化形式添加到数据库中,但是在选择显示日期时,出现以下错误:

Notice: unserialize() [function.unserialize]: Error at offset 0 of xxx bytes in ...

Here is my code: 这是我的代码:

1st .php file here to take a form input (a date) and then get a list of each date over a 12 week period from the start date, and insert into the DB: 这里的第一个.php文件采用表单输入(日期),然后获取从开始日期起12周内每个日期的列表,然后插入数据库中:

The array: 数组:

$start = strtotime($_POST['Start_Date']);
$dates=array();
for($i = 0; $i<=84; $i++)
{
    array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}

$savetodb = serialize($dates);

The insert: 插入:

$sql = "INSERT INTO programme VALUES (NULL, '20', '".$_POST["Start_Date"]."' , ' ".$savetodb." ', '".$_POST["Programme_Notes"]."')"; 

2nd .php file here - SELECT and unserialize: 第二个.php文件在这里-选择并反序列化:

$result = mysql_query("SELECT Programme_Dates FROM programme");

while($row = mysql_fetch_array($result))
  {
  $dates = unserialize($row["Programme_Dates"]); 
  echo $dates;

  }

From what I've read the problem could be related to the DB column where the serialized array is inserted (ie being too small), but it is set to TEXT so that should be fine right? 从我所读的内容来看,问题可能与插入序列化数组的DB列有关(即,它太小了),但是它设置为TEXT,所以应该没事吧? I also thought there may be certain characters within a date causing problems, but when testing with a "regular" array (ie just text), I get the same errors. 我还认为日期内可能有某些字符会引起问题,但是在使用“常规”数组(即仅文本)进行测试时,会遇到相同的错误。

Any suggestions / hints much appreciated, thanks. 任何建议/提示非常感谢,谢谢。

Why are you using stripslashes? 为什么要使用反斜杠? My bet is that is the problem. 我敢打赌,这就是问题所在。 Remove that from there and see if it works. 从那里删除它,看看是否可行。

As a side note, stripslashes should be avoided as if data is probably inserted into the database they should be escaped properly meaning no extra slashes should be added. 附带说明一下,应避免使用反斜杠,因为数据很可能已插入数据库中,因此应正确转义,这意味着不应添加额外的斜杠。 If you need to stripslashes from the data itself I would suggest using something like array_filter after you unserialized the array. 如果您需要从数据本身中去除反斜杠 我建议您在对数组进行反序列化使用诸如array_filter之类的东西。

EDIT 编辑

You should also look into SQL Injection and how to prevent it, as your code is suseptible to be exploited. 您还应该研究SQL注入以及如何防止它,因为您的代码很容易被利用。

UPDATE UPDATE

Looking further at your code you insert the serialized array with 2 extra spaces: ' ".$savetodb." ', 进一步查看您的代码,您将在序列化数组中插入2个额外的空格: ' ".$savetodb." ', ' ".$savetodb." ', try using just '".$savetodb."', that and see if it fixes your issue. ' ".$savetodb." ',尝试仅使用'".$savetodb."',然后查看是否可以解决您的问题。

i have found that the serialize value stored to database is converted to some other way format. 我发现将存储到数据库的序列化值转换为其他方式的格式。 Since the serialize data store quotes marks, semicolon, culry bracket, the mysql need to be save on its own, So it automatically putting "backslash()" that comes from gpc_magic_quotes (CMIIW). 由于序列化数据存储用引号,分号,副词括号括起来,因此mysql需要单独保存,因此它会自动放置来自gpc_magic_quotes(CMIIW)的“ backslash()”。 So if you store a serialize data and you wanted to used it, in the interface you should used html_entity_decode() to make sure you have the actual format read by PHP. 因此,如果您存储序列化数据并希望使用它,则在界面中应使用html_entity_decode()来确保您具有PHP读取的实际格式。

here was my sample: 这是我的样本:

$ser = $data->serialization; // assume it is the serialization data from database
$arr_ser = unserialize(html_entity_decode($ser));

nb : i've try it and it works and be sure avoid this type to be stored in tables (to risky). nb:我已经尝试过并且可以正常工作,请确保避免将这种类型的数据存储在表中(有风险)。 this way can solve the json format stored in table too. 这种方式也可以解决存储在表中的json格式。

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

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