Home » Php » html – How to insert data to mysql database in php – PHP version: 7.3.6

html – How to insert data to mysql database in php – PHP version: 7.3.6

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have coded for insert data to the MySQL database.
It is working correctly and inserted data in the localhost. But after hosted to a real host.data is not inserted to the database.
PHP version: 7.3.6
****Please help me to fix it.****
that code as follows

Data form Code

<form action="../form_script/cus_insert.php" method="post">
  <input type="text" class="form-control mb-2" placeholder="Name" name="c_name" required>
  <input type="text" class="form-control mb-2" placeholder="Address" name="address" required>
  <input type="text" class="form-control mb-2" placeholder="Contact No" name="contact_no1" required> 
  <input type="text" class="form-control mb-2" placeholder="Contact No" name="contact_no2">
  <input type="text" class="form-control mb-2" placeholder="Email" name="email">
  <br>
  <button class="btn btn-primary" name="submit">Submit</button>
  <input type="reset" name="Cancel" class="btn btn-danger">                                
</form>

Also insert code as follows :

<?php
session_start();
?>
<?php
    require_once("../inc/conn.php");
    if(isset($_POST['submit']))
    {
        $c_name = $_POST['c_name'];
        $address = $_POST['address'];
        $contact_no1 = $_POST['contact_no1'];
        $contact_no2 = $_POST['contact_no2'];
        $email = $_POST['email'];
        $login_user=$_SESSION['s_name'];
        date_default_timezone_set("Asia/colombo");
        $added_date=date("y/m/d");
        $added_time=date("h:i:sa");

        $check=mysqli_query($con,"SELECT * FROM customer WHERE contact_no1='$contact_no1' OR contact_no2='$contact_no2' OR email='$email' ");
        $checkrows=mysqli_num_rows($check);
        if($checkrows>0){
            header("location:../forms/customer_add.php?msg=Duplicate Contact Or Email !");
        }
        else{
            $query = " insert into customer(added_date,added_time,c_name,address,contact_no1,contact_no2,email,add_user,status) values('$added_date','$added_time','$c_name','$address','$contact_no1','$contact_no2','$email','$login_user','1')";
            if(mysqli_query($con,$query))
            {
                header("location:../forms/customer_add.php?msg=Successfully Saved !");

            } else {
                echo "not inserted"; 
            }  
        }
    }
?>
How to&Answers: