当我的插入运行时,它将在文本框中写入文本,但如果我选择“其他”以外的单选按钮选择,则不会在表中插入任何内容。 如果我删除“其他”代码,则选择的单选按钮将写入表格。
这是代码
<input type="radio" name="job" value="PHP Programmer">PHP Programmer
<input type="radio" name="job" value="SQL Programmer">SQL Programmer<br>
<input type="radio" name="job">Other <input type="text" name="job" >
<h3>* If yes, check which ISO standard(s) you are accredited?</h3>
<input type="radio" name="iso_standard" value="17020">17020
<input type="radio" name="iso_standard" value="17025">17025
<br />
<input action="submit" type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['job']) && isset($_POST['iso_standard']))
{
$job = mysqli_real_escape_string($db, $_POST['job']);
$iso_standard =mysqli_real_escape_string($db, $_POST['iso_standard']);
$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
if(!mysqli_query($db, $sql))
{
die('Error: ' .mysqli_error($db));
}
echo "1 record added";
}
else
{
echo "You didn't choose all the options!
}
?>
答案 0 :(得分:0)
您正在使用文本输入字段覆盖作业的值。如果它有任何价值,它就不会发生。
请将您的<input type="text" name="job" >
重命名为otherJob
,您应该没问题
编辑
更改<input type="radio" name="job">Other <input type="text" name="job" >
到<input type="radio" name="job" value="other">Other <input type="text" name="otherJob">
更改$job = mysqli_real_escape_string($db, $_POST['job']);
到
$job = mysqli_real_escape_string($db, $_POST['job']);
$otherJob = mysqli_real_escape_string($db, $_POST['otherJob']);
if ($job == 'other') {
$jobField = $otherJob;
} else {
$jobField = $job;
}
并更改$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
到$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$jobField', '$iso_standard')";