Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm a beginner in PHP, HTML, and Apache. When I run the file, the XAMPP has a notice:

Fatal error: Uncaught mysqli_sql_exception: Column 'firstname' cannot be null in C:\xampp\htdocs\connect\connection.php:17 Stack trace: #0 C:\xampp\htdocs\connect\connection.php(17): mysqli_stmt->execute() #1 {main} thrown in C:\xampp\htdocs\connect\connection.php on line 17

I have two files, one in HTML and one in PHP.

What I have tried:

I double-checked the database on XAMPP but I don't know what's wrong with it.

HTML code:
<!DOCTYPE html>
<!--
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this template
-->
<!DOCTYPE html>
<html>
<head>
  <title>Sign-In Form</title>
  <style>
    body {
      font-family: Arial, sans-serif;
    }
    
    .container {
      max-width: 400px;
      margin: 0 auto;
      padding: 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #f2f2f2;
    }
    
    .form-group {
      margin-bottom: 15px;
    }
    
    label {
      display: block;
      margin-bottom: 5px;
    }
    
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    select {
      width: 100%;
      padding: 8px;
      border-radius: 3px;
      border: 1px solid #ccc;
    }
    
    input[type="submit"] {
      background-color: #4CAF50;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 5px;
      cursor: pointer;
    }
    
    input[type="submit"]:hover {
      background-color: #45a049;
    }
  </style>
</head>
<body>
   
  <div class="container">
    <h2>Sign-In Form</h2>
    <form action="connect.php" method="post" >
      <div class="form-group">
        <label for="lastname">Last Name:</label>
        <input type="text" id="lastname" name="lastname" required>
      </div>
      <div class="form-group">
        <label for="firstname">First Name:</label>
        <input type="text" id="firstname" name="firstname" required>
      </div>
      <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
      </div>
        <div class="form-group">
            <label for="password">Password:</label>
            <input type="text" id="password" name="password" required>
        </div>
      <div class="form-group">
        <label for="contact">Contact Number:</label>
        <input type="number" id="contact" name="contact" required>
      </div>
      <div class="form-group">
        <label for="address">Address:</label>
        <input type="text" id="address" name="address" required>
      </div>
      <div class="form-group">
        <label for="country">Country:</label>
        <input type="text" id="country" name="country" required>
      </div>
        
    </form>
 
   <input type="submit" onClick="myFunction()"/>
         <script>
      function myFunction() {
        window.location.href="https://shopee.ph/?gclid=CjwKCAjwyqWkBhBMEiwAp2yUFhWMuFTyZ0Ev3ju0oygGkwXmdptKzZimCT5xr1Wq-lTekgL3xLtbIRoCzH0QAvD_BwE";
      }
   </script>
    </div>
</body>
</html>



PHP code:
<?php
$lastname = filter_input(INPUT_POST, 'lastname');
$firstname = filter_input(INPUT_POST, 'firstname');
$email = filter_input(INPUT_POST, 'email');
$password = filter_input(INPUT_POST, 'password');
$contact = filter_input(INPUT_POST, 'contact');
$address = filter_input(INPUT_POST, 'address');
$country = filter_input(INPUT_POST, 'country');

$conn = new mysqli('localhost', 'root', '', 'entries');
if($conn->connect_error){
    die('Connection Failed : ' . $conn->connect_error);
} else{
    $stmt = $conn->prepare("insert into entries_(lastname, firstname, email, password, contact, address, country)
            values(?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssiss", $lastname, $firstname, $email, $password, $contact, $address, $country, );
    $stmt->execute();
    echo "sign up successful";
    $stmt->close();
    $conn->close();
}   
?>
Posted
Updated 15-Jun-23 2:02am

Validate your data before you try to submit it: the error is pretty clear:
Error
Column 'firstname' cannot be null
You fetch the value from the user page, but you don't try to check if the user entered anything.
If he didn't, $firstname will be null, and you get an error because the DB is not setup to permit nulls in that column.
 
Share this answer
 
OriginalGriff is spot on, your data table with a field named 'firstname' is configured to accept a value otherwise it will return as invalid. To check if each of your variables has a value, you can run -
$lastname = filter_input(INPUT_POST, 'lastname');
$firstname = filter_input(INPUT_POST, 'firstname');
$email = filter_input(INPUT_POST, 'email');
$password = filter_input(INPUT_POST, 'password');
$contact = filter_input(INPUT_POST, 'contact');
$address = filter_input(INPUT_POST, 'address');
$country = filter_input(INPUT_POST, 'country');

if (empty($lastname) || empty($firstname) || empty($email) || empty($password) || empty($contact) || empty($address) || empty($country)) {
    // At least one variable is empty
    echo "Please fill in all the required fields.";
    return;
}

// All variables have values, continue with the rest of your code


The above will return at the line of code, in your case '$firstname = filter_input(INPUT_POST, 'firstname');' as an invalid entry will stop execution of further code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900