我有一个html表单,可以选择多个值。当我发布值时,值必须存储在列下的数据库表中,作为用逗号分隔的文本。但它会抛出如下错误。 错误:SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以便在#1;'移动硬件,Android',' Android SDK',# 39;印地语','是','是','',' 1234')'在第2行
我的HTML:
<label for="cc">Any 2 Certification courses</label>
<input name="cc" type="text" placeholder="course1,course2">
<br>
<label for="pls">Programming Language Skills</label>
<select multiple name="pls" class="pure-input-1-2">
<option>C</option>
<option>C++</option>
<option>Java</option>
<option>HTML</option>
<option>Javascript</option>
<option>CSS</option>
<option>PHP</option>
<option>SQL</option>
<option>.NET</option>
<option>Android SDK</option>
<option>others</option>
</select>
<br>
我的php脚本:
$pls = mysqli_real_escape_string($con, $_POST['pls']);// here pls(prog lang skills) is a column in table with the type text, do i have to make it a varchar?
$sql="INSERT INTO $tbl_name (name, regno,email,phone,dept,yop,gender,ten,twelve,diploma,cgpa,soa,hoa,cc,pls,cls,night,bpo,marketing,password)
VALUES('$name','$regno','$email','$phone','$dept','$yop','$gender','$ten','$twelve','$diploma','$cgpa','$soa',''$hoa','$cc','$pls',
'$cls','$night','$bpo','$marketing', '$password')";
答案 0 :(得分:2)
您需要使用$_POST['pls']
foreach()
返回的数组
$str = '';
foreach($_POST['pls'] as $vals) {
$str .= $vals . ' ';
}
$newstr = mysqli_real_escape_string($con, $str);
echo rtrim($newstr, ' '); //strips out last space
或者在使用mysqli_real_escape_string()
答案 1 :(得分:2)
您的选择名称应该类似于PHP中的数组以访问多个值,请更改您的选择名称,如下所示:
<select multiple name="pls[]" class="pure-input-1-2">
否则您将只获得一个值。