0
<?php
$conn = mysqli_connect("localhost","root","","furniture");
$vehicle= $_POST['vehicle'];
if($_POST["Submit"] == "Submit")
{
    for($i=0; $i<sizeof($vehicle);$i++)
    {
        $query="INSERT INTO events (status) VALUES ('".$vehicle[$i]."')";  mysqli_query($query) or die (mysqli_error());
    }
    echo "updated";
}
?>
<html>
  <body>
    <form action="test.php" method="post">
      <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
      <input type="submit" value="Submit">
    </form>

  </body>
</html>

I'm getting this error:

Undefined index: Submit in C
Undefined index: vehicle in C

O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • Your code is vulnerable to SQL injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. Never insert unsanitised data directly into your SQL. – ADyson Sep 25 '17 at 16:06
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – iainn Sep 25 '17 at 16:06
  • Have you checked what `$_POST` actually contains? – Carcigenicate Sep 25 '17 at 16:07
  • Check your $_POST variable like `print_r $_POST;`. Then you will find your error quite fast. – S.Gartmeier Sep 25 '17 at 16:07
  • It means there is no variable with that value – Robert Sep 25 '17 at 16:07
  • submit works only when you click submit button. When the form is loaded, the button is not clicked, so it will show the error – Anandhu Nadesh Sep 25 '17 at 16:10
  • use if(isset($_POST['submit'])) – Anandhu Nadesh Sep 25 '17 at 16:11

2 Answers2

0

You missed a parameter in mysqli_query. Put this : mysqli_query($conn,$query)

and also check whether your $_POST contains an array or not by var_dump

Herry Potei
  • 181
  • 13
0

Change ur code like this

if(isset($_POST["Submit"]))
{
    $vehicle= $_POST['vehicle'];
    for($i=0; $i<sizeof($vehicle);$i++)
    {
        $query="INSERT INTO events (status) VALUES ('".$vehicle[$i]."')";  mysqli_query($query) or die (mysqli_error());
    }
    echo "updated";
}
Anandhu Nadesh
  • 672
  • 2
  • 11
  • 20