尝试通过 foreach 循环向数组添加值时遇到问题。基本上我有这个表格,其中有一堆主题,用户可以点击“喜欢”或“不喜欢”,每个主题有两个单选按钮。然后我想在两个单独的数组中收集“喜欢”和“不喜欢”,并将它们插入到数据库中,但我没有做正确的事情。这是来自 HTML 代码的示例:
Action movies Like<input type="radio" name="1" value="1" /> Dislike<input type="radio" id="2" name="1" value="2" />
和 PHP 代码:
if (isset($_POST['submit'])) {
$likes = array();
$dislikes = array();
foreach($_POST as $key => $value) {
/* $key is the name of the object an user can click "like" or "dislike" on, $value
is either 'like', which is equal to '2' or 'dislike', equal to '1' */
if ($value > 1) { array_push($likes, $key); } else { array_push($dislikes, $key);
}
}
echo 'The object(s) the user likes: ' . $likes . ' ,
and the object(s) the user dislikes: ' . $dislikes;
然而,我收到了这个:
“用户喜欢的对象:Array ,用户不喜欢的对象:Array”
我敢肯定这只是我在这里犯的一些愚蠢的错误。我已经用谷歌搜索了很多,但我找不到任何对我有帮助的解决方案。