我正在使用数据库中的值填充多选表单。我想做的是根据我提供的数据选择一些值。我的代码有语法错误
这是我的代码:
<?php
$des_pos_id = $_POST['des_pos'];
$test_tags = $_POST['test_tag_arr'];
$test_tag=explode(',',$test_tags);
$sql4 = "SELECT _id, score_type from test_category where sub_code='$des_pos_id'";
$sel4 = mysqli_query($connect,$sql4);
while($row4 = mysqli_fetch_assoc($sel4)){
$option1 .= '<option value = "'.$row4['_id'].'"'echo in_array($row4['_id'], $test_tag) ? 'selected="true"' : null;'>'.$row4['score_type'].'</option>';
}
$output = "<select name='test_tags[]' id='test_tags[]' multiple>";
$output .= $option1;
$output .= '</select> ';
echo $output;
exit;
?>
这是错误信息:
syntax error, unexpected 'echo' (T_ECHO) in /Applications/XAMPP/xamppfiles/htdocs/temp/esco-quiz/controller/script_controller.php
最佳答案
正如戈登·利诺夫所说
Note: Because Echo is a language construct and not a function, it cannot be called using variable functions. Manual
Echo 不会返回任何内容,因此您不能像使用 remove echo from .$row4['_id'].'"'echo in_array($row4['_id'],
这里
并且您的代码容易受到 sql 注入(inject)的攻击...并且使用 mysqli 扩展并不意味着您可以安全地进行 sql 注入(inject),您需要正确地转义/清理所有请求
所以要么使用 mysqli_real_escape_string
要么使用准备好的语句
(更好)
关于php - 从数据库语法错误中使用(select=true)表单填充多个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24050723/