Mcolo
Mcolo

Reputation: 149

I am trying to insert html form data in database through php but it's not happening

I am trying to insert html form data in database through php. I am unable to insert html form data into database , i donot know , what i am doing wrong in my code?

Here is my code:

<?php
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'mydb';
$con=mysqli_connect($host, $username, $password,$dbname);
if (!$con) 
{
    echo "connection failed";
}

if(isset($_POST['submit']))
{
 $name = $_POST['username'];
 $pass = $_POST['password'];
$sql="insert into mytable1 (name,password) VALUES ('$name', '$pass')";
    if(mysqli_query($con,$sql))
    {
        echo "DATA IS INSERTED INTO DATABASE";
    }
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>SignUpform.com</title>

    </head>
    <body>

        <div id="div1">
            <div id="div2">
                <h1>Sign up Form</h1>
            </div>
           <form action="" method="post">
            <fieldset>
                <legend>Signup form ceredentials</legend>
                 <p id="p1">Name:</p>    
                <input id="text1" type="text" placeholder="Enter username" name="username">
                 <p id="p2">Password:</p>
                <input id="pass1" type="password" placeholder="Enter Password" name="password">
                <p id="email">Email:</p>
                <input id="emailtextfld" type="text" placeholder="Enter Email" name="email">
                <p id="company">Company Name:</p>
                <input id="companytextfld" type="text" placeholder="Company name" name="company">
                <input id="donebtn" type="submit" value="submit" name="submit">

            </fieldset>
           </form> 
        </div>
    </body>
</html>

And here is my database enter image description here

The structure of my database table: enter image description here

Upvotes: 1

Views: 187

Answers (3)

Shahab Khan
Shahab Khan

Reputation: 542

You should change your storage engine of your database from:

ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8mb4;

to

ENGINE=innoDB DEFAULT CHARSET=utf8;

From my point of view it's happening just because of "Engine error".So after changing your storage engine of your database , i hope it will work for you inshallah.

Upvotes: 2

Shibon
Shibon

Reputation: 1574

You need to give form action like this

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

add this to check connection

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

Upvotes: 1

Prakhar Sood
Prakhar Sood

Reputation: 179

if(isset($_POST['submit']))
{
$name = $_POST['username'];
$pass = $_POST['password'];
$sql="insert into mytable1 (name,password) VALUES ('$name', '$pass')";

$query = mysqli_query($con, $sql);
if($query)
{
echo "<h4 style='color:green'>Inserted Successfully.</h4>";
    }
    else
    {
        echo "<h4 style='color:red'>Faild.</h4>";
    }
}
?>

Try this solution.

Upvotes: 2

Related Questions