简体   繁体   中英

Insert Data into MySQL Database Using Radio Button in PHP doesnt work properly undefined index

these are my forms:

<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&reg; Core&trade; i3 4130 3,4 GHz</br>
        <Input type = 'Radio' Name ='processor' value= 'processor2'>Intel&reg; Core&trade; i5 4670K 3.40 Ghz </br>
        <Input type = 'Radio' Name ='processor' value= 'processor3'>Intel&reg; Core&trade; i7 4770K 3.5 GHz </br>

        </FORM>`

this is the radiobutton.php file

<?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.';
}
?>

i keep getting the undefined index notice,the auto increment works but the info that normally is added by the radiobuttons isnt added in my database.

You're using separate forms for each set of radio buttons, and none of them have a submit button. Only the inputs within the form that is being submitted will be passed to the server. So $_POST['behuizing'] , $_POST['voeding'] , and $_POST['processor'] are not defined, giving you that error. Your form should look like this:

<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&reg; Core&trade; i3 4130 3,4 GHz<br />
    <Input type = 'Radio' Name ='processor' value= 'processor2' />Intel&reg; Core&trade; i5 4670K 3.40 Ghz <br />
    <Input type = 'Radio' Name ='processor' value= 'processor3' />Intel&reg; Core&trade; i7 4770K 3.5 GHz <br />

    <Input type = 'submit' Name ='submit' value= 'Submit' ><br />

</FORM>

Also, to avoid the error in the event someone doesn't check any of the buttons on a particular set (like processor), you can use isset() like this:

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM