1

I have written a HTML form filling page(sample.html), and also I have written a PHP (sample.php) code for inserting data from HTML page , but i could not receive the Correct message to me, please explain anybody else!!!

Here is the php code:-

<?php

$connection = mysql_connect('localhost', 'root','');

if (!$connection){
    die("Database Connection Failed" . mysql_error());
}

$select_db = mysql_select_db( "selva",$connection);

if (!$select_db){
    die("Database Selection Failed" . mysql_error());
}

error_reporting(0);

session_start();

$first_name = $_POST['first_name'];
$father_name = $_POST['father_name'];
$gender = $_POST['gender'];
$date_of_birth = $_POST['date_of_birth'];
$sports = $_POST['sports'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];

if($first_name!='' and $father_name!='' and $gender!='' and $date_of_birth!='' and $sports!='' and $mobile!='' and $email!='') {

     $query = mysql_query("insert into register(first_name, father_name, gender, date_of_birth, sports, mobile, email) values('$first_name', '$father_name', '$gender', '$date_of_birth','$sports','$mobile','$email')");

     echo "<br/><br/><span>Data Inserted successfully...!!</span>";

 } else {
     echo "<p>Failed to Insert data <br/> Some Fields are Blank....!!</p>";
 }

 mysql_close($connection); 

 ?>

Please tell how to insert the data from HTML page to database??

tomloprod
  • 7,472
  • 6
  • 48
  • 66

1 Answers1

0

try to this..

use <form action="sample.php" method="post">

<?php

$connection = mysqli_connect('localhost', 'root','');

if (!$connection){
    die("Database Connection Failed" . mysql_error());
} else {
    $select_db = mysqli_select_db( "selva",$connection);

    if (!$select_db){
        die("Database Selection Failed" . mysql_error());
    } else {
        error_reporting(0);

        session_start();

        $first_name = $_POST['first_name'];
        $father_name = $_POST['father_name'];
        $gender = $_POST['gender'];
        $date_of_birth = $_POST['date_of_birth'];
        $sports = $_POST['sports'];
        $mobile = $_POST['mobile'];
        $email = $_POST['email'];

        if($first_name!='' and $father_name!='' and $gender!='' and $date_of_birth!='' and $sports!='' and $mobile!='' and $email!='') {

           mysqli_query($connection, "insert into register(first_name, father_name, gender, date_of_birth, sports, mobile, email) values('$first_name', '$father_name', '$gender', '$date_of_birth','$sports','$mobile','$email')");

            if(mysqli_affected_rows($connection) > 0){
                echo "<p>Data Successfully add Added</p>";
            } else {
                echo "Employee NOT Added<br />";
                echo mysqli_error ($connection);
            }
         } else {
             echo "<p>Failed to Insert data <br/> Some Fields are Blank....!!</p>";
         }

         mysql_close($connection);
    }
}

?>
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
  • Reffer this link. i think this is usefull for you... https://www.eduonix.com/blog/web-programming-tutorials/learn-submit-html-data-mysql-database-using-php/ – Pravin Vavadiya Nov 04 '16 at 11:05