Anuj TBE
Anuj TBE

Reputation: 9800

Values are not inserting in MySQL form from php form even after successful submission

I am new to PHP and MySQL. I created a php form to save some data to the databse table. After submitting the form, it gives

"1 recorded added"

as specified in the php insert string. But after opening the table in phpmyadmin, it shows a new row added but no data in it..

The php form is:

<h2>Register Yourself</h2>
            <form method="post" action="get-member.php">
                <div>
                    <span><p>First Name:</p></span>
                    <span><input type="text" class="form-control" id="FirstName"></span>
                </div>
                <div>
                    <span>Last Name:</span>
                    <span><input type="text" class="form-control" id="LastName"></span>
                </div>
                <div>
                    <span>Father's Name: </span>
                    <span><input type="text" class="form-control" id="FatherName"></span>
                </div>
                <div>       
                    <span>Mother's Name: </span>
                    <span><input type="text" class="form-control" id="MotherName"></span>
                </div>
                <div>           
                    <span>Date of Birth: </span>
                    <span><input type="date" class="form-control" id="DOB"></span>
                </div>
                <div>
                    <span>Address: </span>
                    <span><input type="text" class="form-control" id="Address"></span>
                </div>
                <div>       
                    <span>City: </span>
                    <span><input type="text" class="form-control" id="City"></span>
                </div>
                <div>   
                    <span>District: </span>
                    <span><input type="text" class="form-control" id="District"></span>
                </div>
                <div>       
                    <span>Postal Code: </span>
                    <span><input type="text" class="form-control" id="PostalCode"></span>
                </div>
                <div>
                    <span>Personal Mobile #:</span>
                    <span><input type="number" class="form-control" id="Pmobile"></span>
                </div>
                <div>   
                    <span>Father's Contact #:</span>
                    <span><input type="number" class="form-control" id="Fmobile"></span>
                </div>
                <div>
                    <span>Mother's Contct #:</span>
                    <span><input type="number" class="form-control" id="Mmobile"></span>
                </div>
                <div>
                    <span>Home contact #:</span>
                    <span><input type="number" class="form-control" id="Hmobile"></span>
                </div>
                <div>
                    <span><input type="submit" value="Register"></span>
                </div>
            </form>

and the get-member.php file is:

<?php


//stablising connection
include("../database/connection.php"); 

//escape variables for security
$FirstName = mysqli_real_escape_string($con, $_POST['FirstName']);
$LastName = mysqli_real_escape_string($con, $_POST['LastName']);
$FatherName = mysqli_real_escape_string($con, $_POST['FatherName']);
$MotherName = mysqli_real_escape_string($con, $_POST['MotherName']);
$DOB = mysqli_real_escape_string($con, $_POST['DOB']);
$Address = mysqli_real_escape_string($con, $_POST['Address']);
$City = mysqli_real_escape_string($con, $_POST['City']);
$District = mysqli_real_escape_string($con, $_POST['District']);
$PostalCode = mysqli_real_escape_string($con, $_POST['PostalCode']);
$Pmobile = mysqli_real_escape_string($con, $_POST['Pmobile']);
$Fmobile = mysqli_real_escape_string($con, $_POST['Fmobile']);
$Mmobile = mysqli_real_escape_string($con, $_POST['Mmobile']);
$Hmobile = mysqli_real_escape_string($con, $_POST['Hmobile']);




$sql="INSERT INTO RUPmembers (FirstName, LastName, FatherName, MotherName, DOB, Address, City, District, PostalCode, Pmobile, Fmobile, Mmobile, Hmobile)

VALUES ('$FirstName','$LastName','$FatherName','$MotherName','$DOB','$Address','$City','$District','$PostalCode','$Pmobile','$Fmobile','$Mmobile','$Hmobile')";

if (!mysqli_query($con,$sql))
{
    die ('Error:' . mysqli_error($con));
}
echo "1 Record addedd";




//closing connection
include("../database/close-connection.php");

?>

The phpmyadmin table after inserting 3 data(s):

[sorry it is requesting to gain 10 repo to be able to add image, but here is the link of the screenshot http://oi60.tinypic.com/n6rtyu.jpg ]

Upvotes: 0

Views: 292

Answers (3)

Sanjaya
Sanjaya

Reputation: 156

To catch data from the html form by php,the name should be given inside the $_post[''] method not the id. use name="FirstName" name="LastName" name="DOB" etc..

it will solve the problem..

Upvotes: 0

mseifert
mseifert

Reputation: 5670

You are using id="FirstName"

You need to have the property name set for each field. For example:

name="FirstName"

To debug this, you should take a look at your $_POST vars using var_dump($_POST) to show what is being passed on from the form.

Note: Queries should be protected from any data received from the user to prevent sql injection. See this post to learn how to avoid this.

Upvotes: 1

user3655857
user3655857

Reputation: 55

Change

input type="text" class="form-control" id="LastName">

to

input type="text" class="form-control" name="LastName">

This should slove your problem however do not insert unmanipulated info from users into your query.

Upvotes: 1

Related Questions