0

I'm trying to insert dropdown values into my database but it won't insert. I'm not getting any errors either so I'm kinda confused, still new and trying to learn PHP :)

So here's a sample of the dropdown:

<div class="wrap-input100 validate-input">
                <span class="label-input100" style="font-size: 17px; color: black;">I lock my computer screen when I walk away from it (Windows + L)</span>
                <select class="selection-2" name="q1">
                    <option selected disabled hidden></option>
                    <option value="Yes">Yes</option>
                    <option value="No">No</option>
                </select>
            </div>

and here's my PHP code (LMK if you need the whole PHP code, only posted these as I thought this is where the errors come from):

if(empty($emailaddress_err) && empty($q1_err) && empty($q2_err) && empty($q3_err) && empty($q4_err) && empty($q5_err) && empty($q6_err) && empty($q7_err) && empty($q8_err) && empty($q9_err) && empty($q10_err)){
    // Prepare an insert statement
    $sql = "INSERT INTO employees (emailaddress, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
     
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "sssssssssss", $emailaddress, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10);
        
        // Set parameters
        $emailaddress = $emailaddress;
        $q1 = $_POST['q1'];
        $q2 = $_POST['q2'];
        $q3 = $_POST['q3'];
        $q4 = $_POST['q4'];
        $q5 = $_POST['q5'];
        $q6 = $_POST['q6'];
        $q7 = $_POST['q7'];
        $q8 = $_POST['q8'];
        $q9 = $_POST['q9'];
        $q10 = $_POST['q10'];
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records created successfully. Redirect to landing page
            header("location: index.php");
            exit();
        } else{
            echo "Something went wrong. Please try again later.";
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}

// Close connection
mysqli_close($link);

}

PHP code is on top of the index HTML so here's my form action:

<form class="contact100-form validate-form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" onsubmit="alert('Success!');">

and the submit button:

<button class="myButton" type="submit">Submit</button>
Shadow
  • 33,525
  • 10
  • 51
  • 64
Renzo
  • 109
  • 6
  • 1
    Have you got error reporting switched on? [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Sep 14 '20 at 11:32
  • @Dharman I don't think I have, actually. Let me try this one. – Renzo Sep 14 '20 at 11:37
  • Are you sure that the code is actually entering the IF block? You might need to so some echo/log lines to track where the code is going. – aynber Sep 14 '20 at 11:37
  • Are you sure the IF statement is correct? It looks like you're saying IF q1 (plus all the other values) ARE EMPTY insert them. But then is there a null condition on the columns in the DB? If there's a "cannot be null" then it won't insert. – user1020496 Sep 14 '20 at 12:04
  • @user1020496 The variables in the `if` statement are for any errors that are set. He's saying if all those errors are empty, continue. – BadHorsie Sep 14 '20 at 12:40

0 Answers0