0
我有這樣的陣列,我試圖更新使用來自form.I發佈的數據更新多維數組
$arr = array(
'one' => array(
'slidertitle' => 'lorem ipsum',
'sliderlocation' => 'http://localhost/images/1.jpg',
'sliderdescription' => 'this is a good lorem ipsum image',
'sliderposition' => 1
),
'two' => array(
'slidertitle' => 'second slider',
'sliderlocation' => 'http://localhost/images/2.jpg',
'sliderdescription' => 'this space was reserved for a link source code here',
'sliderposition' => 2
),
'three' => array(
'slidertitle' => 'third slider',
'sliderlocation' => 'http://localhost/images/3.jpg',
'sliderdescription' => 'this is a third slider by name only',
'sliderposition' => 3
),
'four' => array(
'slidertitle' => 'fourth slider',
'sliderlocation' => 'http://localhost/images/4.jpg',
'sliderdescription' => 'fourth slider has a description',
'sliderposition' => 4
),
'five' => array(
'slidertitle' => 'fifth slider',
'sliderlocation' => 'http://localhost/images/5.jpg',
'sliderdescription' => 'a slider on rails is really nice',
'sliderposition' => 5
),
'six' => array(
'slidertitle' => 'sixth slider',
'sliderlocation' => 'http://localhost/images/6.jpg',
'sliderdescription' => 'this is the sixth slider,like,really!',
'sliderposition' => 6
)
);
沒有通過$ _ POST數據,因爲它只是一個例子消毒。
$clickedButton = $_POST['clickedButton'];
$sliderKey = $_POST['sliderKey'];
$sliderTitle = $_POST['sliderTitle'];
$sliderDescription = $_POST['sliderDescription'];
$sliderLocation = $_POST['sliderLocation'];
$sliderPosition = $_POST['sliderPosition'];
$result = mysql_query("SELECT * FROM arr");
$row = mysql_fetch_array($result);
$data = $row['encoded_array'];
//echo $data.'<br/>'.'<br/>';
$unserialized = unserialize($data);
$unserialized[$sliderKey]["slidertitle"] = $sliderTitle;
$unserialized[$sliderKey]["sliderlocation"] = $sliderLocation;
$unserialized[$sliderKey]["sliderdescription"] = $sliderDescription;
$unserialized[$sliderKey]["sliderposition"] = $sliderPosition;
//print_r($unserialized);
$newSerialize = serialize($unserialized);
mysql_query("UPDATE arr SET encoded_array = '$newSerialize'");
當我print_r $unserialized
,我剛纔發佈的數據是存在的,但沒有提交到數據庫,我serialize.Is有什麼我做錯了之後?
編輯:
我在陣列中的兩個記錄,這是我得到節省
a:2:{s:13:"5056d218631bc";a:4:{s:11:"slidertitle";s:5:"lorem";s:14:"sliderlocation";s:5:"ipsum";s:17:"sliderdescription";s:11:"hello world";s:14:"sliderposition";i:3;}s:13:"5056db269305c";a:4:{s:11:"slidertitle";s:1:"A";s:14:"sliderlocation";s:6:"RECORD";s:17:"sliderdescription";s:18:"WITH A DESCRIPTION";s:14:"sliderposition";s:1:"2";}}
系列化之前:
Array
(
[5056d218631bc] => Array
(
[slidertitle] => lorem
[sliderlocation] => ipsum
[sliderdescription] => hello world
[sliderposition] => 3
)
[5056db269305c] => Array
(
[slidertitle] => A
[sliderlocation] => RECORD
[sliderdescription] => WITH A DESCRIPTION
[sliderposition] => 2
)
)
如果你在數據庫插入之前使用var_dump($ newSerialize),你會得到什麼? – JamesHalsall
我發佈的數據就在那裏。 – Gandalf
請不要使用'mysql_ *'函數,它已被棄用(請參閱[*紅盒子*](http://php.net/manual/en/function.mysql-query.php)),並且容易受到sql注入。使用[* PDO *](http://php.net/manual/en/book.pdo.php)或[* MySQLi *](http://php.net/manual/en/book.mysqli.php) 。 – alfasin