Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP inserting twice?

220061

Well-Known Coder
hello I'm trying to find out why my db inserts the same row twice., I just can't seem to find out why it does this?
PHP:
<?php
  include "navbar.php";
  include "config.php";
?>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<section class="vh-100" style="background-color: #eee;">
  <div class="container h-100">
    <div class="row d-flex justify-content-center align-items-center h-100">
      <div class="col-lg-12 col-xl-11">
        <div class="card text-black" style="border-radius: 25px;">
          <div class="card-body p-md-5">
            <div class="row justify-content-center">
              <div class="col-md-10 col-lg-6 col-xl-5 order-2 order-lg-1">
                <p class="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">Sign up</p>
                <form class="mx-1 mx-md-4" action="registerback.php" method="post">
                  <div class="d-flex flex-row align-items-center mb-4">
                    <div class="form-outline flex-fill mb-0">
                      <input type="text" id="name" class="form-control" name="name"/>
                      <label class="form-label" for="name" id="name">Your Name</label>
                    </div>
                  </div>
                  <div class="d-flex flex-row align-items-center mb-4">
                    
                    <div class="form-outline flex-fill mb-0">
                      <input type="text" id="username" class="form-control" name="username"/>
                      <label class="form-label" for="username" id="username">Your username</label>
                    </div>
                  </div>
                  <div class="d-flex flex-row align-items-center mb-4">
                    
                    <div class="form-outline flex-fill mb-0">
                      <input type="email" id="email" class="form-control" name="email"/>
                      <label class="form-label" for="email" id="email">Your Email</label>
                    </div>
                  </div>
                  <div class="d-flex flex-row align-items-center mb-4">
                    
                    <div class="form-outline flex-fill mb-0">
                      <input type="password" id="password" class="form-control" name="password"/>
                      <label class="form-label" for="password" id="password">Password</label>
                    </div>
                  </div>
                  <div class="form-check d-flex justify-content-center mb-5">
                    <input
                      class="form-check-input me-2"
                      type="checkbox"
                      value=""
                      id="form2Example3c"
                    />
                    <label class="form-check-label" for="form2Example3">
                      I agree all statements in <a href="#!">Terms of service</a>
                    </label>
                  </div>
                  <div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
                    <button class="btn btn-primary btn-lg" type="submit">Register</button>
                  </div>
                </form>
              </div>
              <div class="col-md-10 col-lg-6 col-xl-7 d-flex align-items-center order-1 order-lg-2">
                <img src="https://mdbootstrap.com/img/Photos/new-templates/bootstrap-registration/draw1.png" class="img-fluid" alt="Sample image">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

backend code

PHP:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_POST["email"]) && $_POST["password"] === true){
    header("location: welcome.php");
    exit;
}
// Include config file
include "config.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = mysqli_real_escape_string($conn , $_POST['email']);
$name = mysqli_real_escape_string($conn , $_POST['name']);
$username = mysqli_real_escape_string($conn , $_POST['username']);
$password = mysqli_real_escape_string($conn , $_POST['password']);
$sql = "INSERT INTO users (email, name, password, username) VALUES (?, ?, ?, ?)";
if($stmt = $conn->prepare($sql)) { // assuming $mysqli is the connection
    $stmt->bind_param('ssss', $email, $name, $password, $username);
    $stmt->execute();
    // any additional code you need would go here.
} else {
    $error = $conn->errno . ' ' . $conn->error;
    echo $error; // 1054 Unknown column 'foo' in 'field list'
}

//schets van w3schools
// prepare and bind
// $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
// $stmt->bind_param("sss", $firstname, $lastname, $email);
if ($stmt->execute()) {
    // it worked
    header("Location: stmtsucces.php");
 } else {
    // it didn't
    header("Location: stmtfailure.php");
 }
$stmt->close();
$conn->close();
}
?>
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom