使用php中的serialize将关联数组存储在数据库中

时间:2014-01-10 04:40:37

标签: php mysql database serialization associative-array

我之前已经问过这个,现在我知道如何序列化它。但现在问题是我遇到了这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''address1t', 'address2t', 'cityt', 'provincet', 'postalCODEt', 'contactNOt', 'em' at line 1

这是我的关联数组:

Array ( [0] => Array ( [recNAME] => NAME1 [company] => [address1] => ADDRESS1 [address2] => [city] => CITY1 [province] => PROVINCE1 [postalCODE] => 0 [contactNO] => 926XXXXX [email] => XXXXX@yahoo.com [commodity] => COMMODITY1 [type] => [weight] => 0 [length] => 0 [width] => 0 [height] => 0 [decVAL] => 0 [specINSTRUC] => [shipREF] => REF1 [payMETHOD] => CashOnDelivery [packNO] => 200XXXX-XXX [trackNO] => 200XXXX-XXX ) [1] => Array ( [recNAME] => NAME2 [company] => [address1] => ADDRESS2 [address2] => [city] => CITY2 [province] => PROVINCE2 [postalCODE] => 0 [contactNO] => 905XXXXXX [email] => XXXXX@hotmail.com [commodity] => COMMODITY2 [type] => [weight] => 0 [length] => 0 [width] => 0 [height] => 0 [decVAL] => 361 [specINSTRUC] => I'm in the above given address from M-F, 11AM-6PM, call or text me for confirmation [shipREF] => 200XXXXX [payMETHOD] => CashOnDelivery [packNO] => 200XXXX-XXX [trackNO] => 200XXXX-XXX ) )

序列化数组看起来像这样:

a:4:{i:0;a:21:{s:7:"recNAME";s:18:" NAME1";s:7:"company";s:0:"";s:8:"address1";s:57:"ADDRESS1";s:8:"address2";s:0:"";s:4:"city";s:8:"CITY1";s:8:"province";s:8:"PROVINCE1";s:10:"postalCODE";s:1:"0";s:9:"contactNO";s:10:"9XXXXXXXX";s:5:"email";s:21:"XXXX@yahoo.com";s:9:"commodity";s:13:"COMMODITY1";s:4:"type";s:0:"";s:6:"weight";s:1:"0";s:6:"length";s:1:"0";s:5:"width";s:1:"0";s:6:"height";s:1:"0";s:6:"decVAL";s:2:"99";s:11:"specINSTRUC";s:0:"";s:7:"shipREF";s:9:"200XXXX";s:9:"payMETHOD";s:14:"CashOnDelivery";s:6:"packNO";s:14:"200XXXX-XXX";s:7:"trackNO";s:14:"200XXXX-XXX";}i:1;a:21:{s:7:"recNAME";s:32:" NAME2";s:7:"company";s:0:"";s:8:"address1";s:52:"ADDRESS1";s:8:"address2";s:0:"";s:4:"city";s:6:"CITY2";s:8:"province";s:12:"PROVINCE2";s:10:"postalCODE";s:1:"0";s:9:"contactNO";s:10:"9XXXXXX";s:5:"email";s:23:"XXXX@hotmail.com";s:9:"commodity";s:7:"Spyder ";s:4:"type";s:0:"";s:6:"weight";s:1:"0";s:6:"length";s:1:"0";s:5:"width";s:1:"0";s:6:"height";s:1:"0";s:6:"decVAL";s:3:"361";s:11:"specINSTRUC";s:83:"I'm in the above given address from M-F, 11AM-6PM, call or text me for confirmation";s:7:"shipREF";s:9:"200XXXXX";s:9:"payMETHOD";s:14:"CashOnDelivery";s:6:"packNO";s:14:"200XXXX-XXX";s:7:"trackNO";s:14:"200XXXX-XXX";}}

这是我的序列化代码:

$newstring = serialize($array);
@mysql_query("INSERT INTO `temp` (`recNAMEt`, `companyt`, 'address1t', 'address2t', 'cityt', 'provincet', 'postalCODEt', 'contactNOt', 'emailt', 'commodityt', 'typet', 'weightt', 'lengtht', 'widtht', 'heightt', 'decVALt', 'specINSTRUCt', 'shipREFt', 'payMETHODt', 'packNOt', 'trackNOt' ) VALUES('', $newstring)") or die('Error: ' . mysql_error()); 

这是我第一次序列化,我仍在处理事情。我仍然不知道出了什么问题,因为这段代码适用于普通数组。我试过这样做:

VALUES('', '', $newstring)")

仍然没有运气。 recNAMEt,companyt等是我表中的列名。请帮忙。谢谢和问候。

2 个答案:

答案 0 :(得分:3)

你错了@nicole

首先序列化将数组转换为字符串

所以

有两种方式

a)在数据库中有一个列,并在该列中插入序列化数组\

例如:

Instead of this

  @mysql_query("INSERT INTO `temp` (`recNAMEt`, `companyt`, 'address1t', 'address2t', 'cityt', 'provincet', 'postalCODEt', 'contactNOt', 'emailt', 'commodityt', 'typet', 'weightt', 'lengtht', 'widtht', 'heightt', 'decVALt', 'specINSTRUCt', 'shipREFt', 'payMETHODt', 'packNOt', 'trackNOt' ) VALUES('', $newstring)") or die('Error: ' . mysql_error()); 


    You can have

  @mysql_query("INSERT INTO `temp` (`details` ) VALUES($newstring)") or die('Error: ' . mysql_error()); 
当你从数据库中获取结果时,

以及后来拥有了这个值,并且没有显示值&你会找回你的数组

b)这有点棘手,但会解决你的目的      这将生成您的查询

             $sql = sprintf(
              'INSERT INTO `temp`(%s) VALUES ("%s")',
               implode(',',array_keys($_newstring )),
               implode('","',array_values($_newstring ))
              );
            mysql_query($sql);

答案 1 :(得分:0)

假设有一个包含多个复选框的表单,用户可以一次选择多个复选框,然后我们需要将这些值以数组格式存储在表的一列中。

请查看以下代码段,了解如何在仅几行代码中完成此操作 - :)

HTML代码

<form action="path_to_action_page" method="post">
<input type="checkbox" name="value[]" value="value1">Value1
<input type="checkbox" name="value[]" value="value2">Value2
<input type="checkbox" name="value[]" value="value3">Value3
<input type="checkbox" name="value[]" value="value4">Value4
<input type="checkbox" name="value[]" value="value5">Value5

用于检索表单数据的PHP代码: -

$data= $_POST['value'];
$values= serialize($data);

将数据插入数据库的代码: -

//数据库连接的代码

$insert_query = "INSERT INTO TABLE_NAME(col_name) VALUES('$values')";
$result_insert = mysql_query($insert_query);

从数据库中检索值的代码: -

$query = "SELECT col_name FROM TABLE_NAME WHERE condition_to_search_the_database";


$result = mysql_query($query);
 while($values = mysql_fetch_array($result)){


$values= unserialize($values['col_name']);



  foreach($values as $value)


 {
  //code to play with $value
  e.g. - print $value;


 }}