hi everyone.

can you help me to check why this error occur and how to fix it?

when i want to insert the data, this error is coming out and data not insert into database.

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'root123');
define('DB_NAME', 'ems');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//code for visitor registration

if (isset($_POST['register']))
{
    $vname = $_POST['ems_visitor_name'];
    $vage = $_POST['ems_visitor_age'];
    $vgender = $_POST['ems_visitor_gender'];
    $vmaritalstat = $_POST['ems_visitor_maritalstatus'];
    $vnationality = $_POST['ems_visitor_nationality'];
    $vcontactnum = $_POST['ems_visitor_phonenumber'];
    $vemailadd = $_POST['ems_visitor_email'];

    $msg=mysqli_query($con,"insert into ems_visitor(ems_visitor_name,ems_visitor_age,ems_visitor_gender,ems_visitor_maritalstatus,ems_visitor_nationality,ems_status_phonenumber,ems_status_email) values('$vname','$vage','$vgender','$vmaritalstat','$vnationality','$vcontactnum','$vemailadd')");

    if($msg)
    {
        echo "<script>alert('Visitor register successfully');</script>";
    }
}

?> <html> <head> <title>Visitor Registration Form</title> <link href="./css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <form name="frmRegistration" method="post" action=""> <div class="demo-table"> <div class="form-head">Sign Up</div>

?> <div class="field-column"> <label>Visitor</label> <div> <input type="text" class="demo-input-box"
                        name="ems_visitor_name"
                        value="<?php if(isset($_POST['ems_visitor_name'])) echo $_POST['ems_visitor_name']; ?>"> </div> </div> <div class="field-column"> <label>Age</label> <div><input type="text" class="demo-input-box"
                    name="ems_visitor_age" 
                    value="<?php if(isset($_POST['ems_visitor_age'])) echo $_POST['ems_visitor_age']; ?>"></div> </div> <div class="field-column"> <label>Gender</label> <div> <select name="ems_visitor_gender" class="demo-input-box"> <option value="">Select Gender</option> <option value="male">Male</option> <option value="female">Female</option> </select> </div> </div> <div class="field-column"> <label>Marital Status</label> <div> <select name="ems_visitor_maritalstatus" class="demo-input-box"> <option value="">Select Status</option> <option value="single">Single</option> <option value="married">Married</option> <option value="divorced">Divorced</option> </select> </div> </div> <div class="field-column"> <label>Nationality</label> <div> <select name="ems_visitor_nationality" class="demo-input-box"> <option value="">Select Your Residency</option> <option value="malaysia">Malaysia</option> <option value="singapore">Singapore</option> <option value="others">Others</option> </select> </div> </div> <div class="field-column"> <label>Contact Number</label> <div> <input type="text" class="demo-input-box"
                        name="ems_visitor_phonenumber"
                        value="<?php if(isset($_POST['ems_visitor_phonenumber'])) echo $_POST['ems_visitor_phonenumber']; ?>"> </div> </div> <div class="field-column"> <label>Email</label> <div> <input type="text" class="demo-input-box"
                        name="ems_visitor_email"
                        value="<?php if(isset($_POST['ems_visitor_email'])) echo $_POST['ems_visitor_email']; ?>"> </div> </div> <div> <input type="submit"
                        name="register" value="Register"
                        class="btnRegister"> </div> </div> </form> </body> </html>

There is an error in your MySQL query.

Firstly, it's important that you escape all of the variables pulled in via $_POST before inserting them into the database to prevent MySQL injection errors. Please look into https://www.php.net/manual/en/mysqli.real-escape-string.php

commented: Thank you for mentioning SQL injection here. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.