基本上,我要求用户用逗号分隔他们的兴趣。我想解析这些,并将它们输入到SQL数据库中。有没有办法根据他们输入的兴趣数量任意解析?到目前为止,这是我的代码:
$interests = $_POST['interests'];
$interests = explode(',', $interests);
答案 0 :(得分:1)
<?php
$interests = $_POST['interests'];
$interests = explode(',', $interests);
foreach ($interests as $interest) {
// INSERT INTO db using $interest. Make sure to properly escape each interest!
// Use PDO and bind parameters to avoid sql injection attacks
}
答案 1 :(得分:1)
$interests = explode(',', str_replace(Array("\r", "\r"), ",", $_POST['interests'])); // the repalce here is to remove any new lines that might have been added if a textarea field used
$interests = array_filter($interests); // weeds out empty entries that might have been crated
foreach($interests AS $interest) {
$interest = trim($interests); // remove extra spaces that might be
// do what you need
}