0

I am trying to use the html post function with php to insert a 1 or 0 into a database depending on whether a user has ticked a checkbox or not, however I get index errors whenever i submit the form, no matter if the boxes are checked or not. How do I prevent this happening? The error i get is "Notice: Undefined index: Adventure Challenge Award in C:\xampp\htdocs\scout\insertbadgeinphp.php on line 15"

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "scout";


$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if (isset($_POST['submit'])) {

if (!$_POST['Adventure Challenge Award'] == null)
{
    $aca = "1";
}
else{
    $aca = "0";
}

}


$sql = "INSERT INTO insertbadgescout (AdventureChallengeBadge)
VALUES ('$aca');";


if ($conn->multi_query($sql) === TRUE) {

    echo "New records inserted successfully");

}
else 
{
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>




<!DOCTYPE html>
<html> 
<head> 
<title>Insert badges scouts have</title> 

</head> 
<body id="body-color"> 
<div id="badges"> 


<form method="POST" action="insertbadgeinphp.php"> 

<input type="checkbox" name="Adventure Challenge Award" value="yes">Adventure Challenge Award<br>

<input id="button" type="submit" name="submit" value="Save Badges"> 
</form> 

</div> 
</body>
<p><a href="Homepage.html"> Home </a> </p> 
</html> 

4 Answers4

0

Please do the below modification in your code and try

<input type="checkbox" name="Adventure_Challenge_Award" value="yes">Adventure Challenge Award<br>

And in your PHP post section

if ($_POST['Adventure_Challenge_Award'] == 'Yes')
{
  $aca = "1";
}
else
{
$aca = "0";
}
Azeez Kallayi
  • 2,567
  • 1
  • 15
  • 19
0

Your $_POST['Adventure Challenge Award'] is not defined, this is not beeing sent from form, because it uses spaces.

Few tips:

  • Do not use spaces in variables (like you do)
  • Use simplistic names that can be read fast and understood easily like (agreementRadio act.)
  • ALways and I mean always escape your strings that you pass to queries... More information here
  • Send you data to another file, to something like Registration.php.
0

Do not use space in your name="my_input". Use undescore instead. You can change :

if (!$_POST['Adventure Challenge Award'] == null)

By

if (!isset($_POST['Adventure Challenge Award']))

To check if a variable exist or not

ewan
  • 276
  • 2
  • 17
0

Try this, and do not use spaces in $_POST['Adventure Challenge Award']

$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "scout";


$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if (isset($_POST['submit'])) {

if (!$_POST['Adventure_Challenge_Award'] == null)
{
    $aca = "1";
}
else{
    $aca = "0";
}

}


$sql = "INSERT INTO insertbadgescout (`AdventureChallengeBadge`)
VALUES ('$aca');";

if ($conn->query($sql)) {
    echo "New records inserted successfully");
} else {
    echo "Error";
}


$conn->close();
?>