0

What I'm trying to do is to store it's value into database. 

This is my code that is actually not working because it fails at try catch block and shows error (exception $e). It just saves nothing at database. How to fix it?

Index.php: https://pastebin.com/cYDdm9Jz

Process.php

<?php
        if(isset($_POST)){
        $var1 = $_POST['var1'];
        $var2 = $_POST['var2'];
        $var3 = $_POST['var3'];
        $var4 = $_POST['var4'];
        $var5 = $_POST['var5'];
        $var6 = $_POST['var6'];
        $var7 = date("Y-m-d H:i:s");
 
        $sql = "INSERT INTO test2 (var1, var2, var3, var4, var5, var6, var7) VALUES(?, ?, ?, ?, ?, ?, ?)";
        $stmtinsert = $db->prepare($sql);
        try {
 
$result = $stmtinsert->execute([$var1, $var2, $var3, $var4, $var5, $var6, $var7]);
if($result) {
echo 'OK.';
} else {
echo 'Error.';
}
 
} catch (Exception $e) {
echo 'error';
die;
    }
}
?>
  • 2
    When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – tjheslin1 May 21 '22 at 11:19

2 Answers2

0
<form action="" method="post" class="contact100-form validate-form" id="check" autocomplete="off">

You did not put action value in your code. You can try putting process.php in your form action value.

mrshiam
  • 13
  • 6
  • Then it gives me an error: Undefined index: var5 in process.php on line 10, Undefined index: var6 in process.php on line 11, error (this is 'error' message from catch block). – qwerty qwerty May 20 '22 at 08:40
0

Add [] to all names Var6 of your checkbox item in index.php. Like below

<p>
 <input id="field_terms" type="checkbox" required name="var6[]" oninvalid="this.setCustomValidity('agree')" oninput="this.setCustomValidity('')"> do you agree?
</p>
<p>
 <input type="checkbox" id="var5" name="var6[]" value="yes"> do you like it?
</p>
<p>
 <input type="checkbox" id="var6" name="var6[]" value="yes"> are you sure?
</p>

Your process.php will be

if (isset($_POST)) {
        $var1 = $_POST['var1'];
        $var2 = $_POST['var2'];
        $var3 = $_POST['var3'];
        $var4 = $_POST['var4'];
        $var5 = $_POST['var5'];
        $var6 = implode(',', $_POST['var6']);
        $var7 = date("Y-m-d H:i:s");

You can check out this link

mrshiam
  • 13
  • 6
  • Undefined index: var6 in process.php on line 10, invalid arguments passed in process.php on line 10. – qwerty qwerty May 20 '22 at 11:16
  • To insert multiple check box data you have to use a loop or you have to save it with comma separation which is shown above. you can *var_dump* $var6 to see your submitted data. – mrshiam May 20 '22 at 11:51