0

I am trying to create a simple website where someone inputs some information in an html form and with php prepared statements the info gets inserted into my database. Although I have checked a lot of times that my inputs are correct and I get no sql errors the data do not get inserted into my database. Can anyone help find out what goes wrong?

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbServername="localhost";
$dbUsername="root";
$dbPassword="";
$dbName="console";

$conn=mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);

if(isset($_POST['submit'])){
    $last=$_POST['lastname'];
    $first=$_POST['firstname'];
    $tel1=$_POST['tel1'];
    if(empty($_POST['tel2'])){ 
        $tel2=null;
    }else{
        $tel2=$_POST['tel2'];
    }
    $address=$_POST['ad'];
    $postcode=$_POST['postcode'];
    $city=$_POST['city'];
    $email=$_POST['email'];
    $job=$_POST['job'];

    $sql = "INSERT INTO customers (tel1,tel2,postcode,lastname,firstname,ad,city,email,job) VALUES (?,?,?,?,?,?,?,?,?)";
    $stmt=mysqli_stmt_init($conn);//create prepared statement
    if (!mysqli_stmt_prepare($stmt,$sql)){  //check if the prepared statement was succesfully prepared
        header("Location: ../necustomer.php?error=sqlerror");
        exit();
    }else{
  mysqli_stmt_bind_param($stmt,"iiissssss",$tel1,$tel2,$postcode,$last,$first,$address,$city,$email,$job); //bind the parameters with the placeholders
    mysqli_stmt_execute($stmt);
    }
     header("Location: ../necustomer.php?signup=success");

}else{
header("Location: ../necustomer.php");
  }

This is my php file.

janblake
  • 11
  • 1
  • 3
    You're blinding assuming everything works. Check for [mysqli errors](http://php.net/manual/en/mysqli.error.php) to see if there's any errors along the way. – aynber Feb 24 '21 at 18:57
  • 1
    Are you sure `$_POST['submit']` is set? – Dharman Feb 24 '21 at 19:30
  • Yes i have echoed all my variables multiple times to be sure, everything seems to be right. I even get redirected to the signup=success link which gets executed only when the statement gets prepared without an error. – janblake Feb 24 '21 at 19:44
  • Have you enabled mysqli error reporting already? [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Feb 24 '21 at 20:11

0 Answers0