这些是我的表单:
<Form name ="formbehuizing" Method ="Post" ACTION ="radiobutton.php">
<Input type = 'Radio' Name ='behuizing' value= 'behuizing1'>Sharkoon VS3-S red </br>
<Input type = 'Radio' Name ='behuizing' value= 'behuizing2'>Sharkoon VS3-S blue </br>
<Input type = 'Radio' Name ='behuizing' value= 'behuizing3'>Sharkoon VS3-S green </br>
</FORM>
<Form name ="formvoeding" Method ="Post" ACTION ="radiobutton.php">
<Input type = 'Radio' Name ='voeding' value= 'voeding1'>Cooler Master G600 EU</br>
<Input type = 'Radio' Name ='voeding' value= 'voeding2'>Cooler Master B700 </br>
</FORM>`<Form name ="formprocessor" Method ="Post" ACTION ="radiobutton.php">
<Input type = 'Radio' Name ='processor' value= 'processor1'>Intel® Core™ i3 4130 3,4 GHz</br>
<Input type = 'Radio' Name ='processor' value= 'processor2'>Intel® Core™ i5 4670K 3.40 Ghz </br>
<Input type = 'Radio' Name ='processor' value= 'processor3'>Intel® Core™ i7 4770K 3.5 GHz </br>
</FORM>`
这是radiobutton.php文件
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
if(login_check($mysqli) == true) {
$behuizing= mysqli_real_escape_string($mysqli, $_POST['behuizing']);
$voeding= mysqli_real_escape_string($mysqli, $_POST['voeding']);
$processor = mysqli_real_escape_string($mysqli, $_POST['processor']);
$sql="INSERT INTO dario_bestelling (behuizing, voeding, processor)
VALUES ('$behuizing', '$voeding', '$processor')";
if (!mysqli_query($mysqli,$sql)) {
die('Error: ' . mysqli_error($mysqli));
}
echo "1 record added";
mysqli_close($mysqli);
} else {
echo 'You are not authorized to access this page, please login.';
}
?>
我不断得到未定义的索引通知,自动增量工作,但通常是由单选按钮添加的信息不添加在我的数据库中。
您为每组单选按钮使用了单独的表单,并且它们都没有提交按钮。只有提交的表单中的输入才会传递给服务器。所以$_POST['behuizing']
, $_POST['voeding']
, $_POST['processor']
是没有定义的,给你这个误差。您的表单应该看起来像这样:
<Form name ="formbehuizing" Method ="Post" ACTION ="radiobutton.php">
<Input type = 'Radio' Name ='behuizing' value= 'behuizing1' />Sharkoon VS3-S red <br />
<Input type = 'Radio' Name ='behuizing' value= 'behuizing2' />Sharkoon VS3-S blue <br />
<Input type = 'Radio' Name ='behuizing' value= 'behuizing3' />Sharkoon VS3-S green <br /><br />
<Input type = 'Radio' Name ='voeding' value= 'voeding1' />Cooler Master G600 EU<br />
<Input type = 'Radio' Name ='voeding' value= 'voeding2' />Cooler Master B700 <br /><br />
<Input type = 'Radio' Name ='processor' value= 'processor1' />Intel® Core™ i3 4130 3,4 GHz<br />
<Input type = 'Radio' Name ='processor' value= 'processor2' />Intel® Core™ i5 4670K 3.40 Ghz <br />
<Input type = 'Radio' Name ='processor' value= 'processor3' />Intel® Core™ i7 4770K 3.5 GHz <br />
<Input type = 'submit' Name ='submit' value= 'Submit' ><br />
</FORM>
同样,为了避免在某人没有检查特定集合(如处理器)上的任何按钮时出现错误,您可以像这样使用isset()
:
if ( isset( $_POST['behuizing'] ) && isset( $_POST['voeding'] ) && isset( $_POST['processor'] ) ) {
$behuizing = mysqli_real_escape_string($mysqli, $_POST['behuizing']);
$voeding = mysqli_real_escape_string($mysqli, $_POST['voeding']);
$processor = mysqli_real_escape_string($mysqli, $_POST['processor']);
$sql="INSERT INTO dario_bestelling (behuizing, voeding, processor)
VALUES ('$behuizing', '$voeding', '$processor')";
if (!mysqli_query($mysqli,$sql)) {
die('Error: ' . mysqli_error($mysqli));
}
echo "1 record added";
} else {
echo "You didn't choose all the options! No record was added. Please choose one option from each category";
}