Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data is not inserted PHP Mysql XAMPP

I’ve searched many hours on the net and found nothing.
Please check my code and let me know if there is any problem and how should I get the error if there is any because I don’t get any and all steps go successful but data is not inserted .

I m using XAMPP by the way.

My db connection source :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php 
$dbhost = 'localhost:3307';
$dbuser = 'root';
$dbpass = '';
$dbname = 'website';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if(!$conn) {
    die("<br><p style='color:red;font-size:35px;' >Database not connected !</p>");
}
?>

My source code :

<?php session_start();
if(!isset($_POST['sb'])){
    echo "<div style='margin:auto;width:max-content;color:red;font-family:IRANSans;margin-top:30px'>Oops! Access denied !</div>";
}
else{
    require "../DB/db.php";
    $fl = $_POST['1'];
    $national_id = $_POST['2'];
    $company = $_POST['3'];
    $phone = $_POST['4'];
    $mail = $_POST['5'];
    $password = $_POST['6'];
    if (empty($fl)){
        header("Location: /register?error=emptyName");
        exit();
    }
    if (empty($national_id)){
        header("Location: /register?error=emptyID");
        exit();
    }
    if (empty($company)){
        header("Location: /register?error=emptyCompany");
        exit();
    }
    elseif (empty($phone)){
        header("Location: /register?error=emptyPhone");
        exit();
    }
    elseif (empty($mail)){
        header("Location: /register?error=emptyEmail");
        exit();
    }
    elseif (!filter_var($mail , FILTER_VALIDATE_EMAIL)) {
        header("Location: /register?error=invalidEmailFormat");
        exit();
    }
    elseif (empty($password)){
        header("Location: /register?error=emptyPass");
        exit();
    }
    else{
        $sql = "SELECT * FROM users WHERE mail = ?";
        $stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt , $sql)){
            header("Location: /register?error=SQLNotPrepare1");
            exit();
        }
        else{
            mysqli_stmt_bind_param($stmt , "s" , $mail);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_store_result($stmt);
            $rowcount = mysqli_stmt_num_rows($stmt);
            if($rowcount > 0){
                header("Location: /register?error=mailTaken");
                exit();
            }
            else{
                $sql = "INSERT INTO users(fname_lname,national_id,company,phone,mail,password) VALUES(?,?,?,?,?,?)";
                $stmt = mysqli_stmt_init($conn);
                if(!mysqli_stmt_prepare($stmt , $sql)){
                    header("Location: /register?error=SQLNotPrepare2");
                    exit();
                }
                else{
                    $_SESSION['starttime'] = time();
                    $hased_pass = password_hash($password , PASSWORD_DEFAULT);
                    mysqli_stmt_bind_param($stmt , "ssssss" , $fl,$national_id,$company,$phone,$hased_pass);
                    mysqli_stmt_execute($stmt);
                    header("Location: /register?register=success");
                    exit();
                }
            }
        }
    }
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}
?>

>Solution :

Here:

mysqli_stmt_bind_param($stmt , "ssssss" ,  $fl,$national_id,$company,$phone,$hased_pass); 

you have 6 s characters, but only 5 parameter variables. Turn on the error reporting as I’ve described above, and PHP will show you an error message about that!

You forgot to supply the $mail variable, by the looks of it. I think

mysqli_stmt_bind_param($stmt ,"ssssss" , $fl, $national_id, $company, $phone, $mail,  $hased_pass); 

is what you intended.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading