My INSERT query won't work

Thank you in advance for anyone who can help with this, anyways. This is a school project and I am trying to insert some information from a form into the mysql database I’ve made. I already know that the connection to the database has been successful, and also that I am able to retrieve information from the form. The problem occurs when I run the site and it gives me the message saying that this ($conn->query($query)) requirement has not been fulfilled, which indicates there is something wrong with the INSERT query.
Here is how my form and php looks like:

<form action="nyttmedlem.php" method="post">
  <input type="hidden" name="dykkerID" value="NULL"/>
  <input type="text" name="fnavn" required>
  <input type="text" name="enavn" required>
  <input type="submit" name="sendinn" value="Send Inn">

// Create connection
$conn = new mysqli($servername, $username, $password, $db);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully";

echo "<br>Du heter $fnavn $enavn";

$query="INSERT INTO dykking.dykker (`dykkerID`, `fornavn`, `etternavn`, `medlemsiden`) VALUES (NULL,'$fnavn','$enavn','$date')";
If ($conn->query($query)){
echo "Ønskene er registrert<br>";
else {
echo "<br>Something went wrong";

I suspect the problem might be in the NULL value or $dato value in which the NULL value is in mysql an auto increment that should be filled in automatically when added to the db.
Thank you for any help, appreciate it!

I suggest to echo the mysql error that occurs instead of “Something went wrong”. (which would be perfect in a production environment but not while developing).

If ($conn->query($query)){
    echo "Ønskene er registrert<br>";
} else {
    echo $conn->error;
1 Like

Are you not being taught prepared statements using mysqli?

If you are passing in a null value, don’t list the column to begin with, it clutters the space and takes up unnecessarily room.

Your error isn’t from the database, it is from php itself. Your form values are:
name=“fnavn” and name=“enavn”, but you are checking:
$_POST[‘fornavn’] and $_POST[‘etternavn’].

Also, your processing code should be above your input.

Lastly, you are trying to insert something everytime you load the page. You might want to ensure the form was submitted before you do that…

    // process form
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service