Skip to content Skip to sidebar Skip to footer

Html Form To Input Data Into Mysql Database

Im trying to get my html form once submitted, then the data gets saved into a mysql database. However once i click the submit button it just takes me to my php code shown in my br

Solution 1:

You can use this code.. Dont use mysql and mysqli combinations.

<divid="contact_form"><formaction="contact_insert.php"method="post">
    Firstname: <inputtype="text"name="firstname">
    Lastname: <inputtype="text"name="surname">
    Email: <inputtype="text"name="email"><inputtype="submit"name="submit"></form></div><?phpif(isset($_POST['submit']))
{
$username="root";
$password="password";
$server="127.0.0.1";
$database="eddiesdb";

$con = mysql_connect($server,$username,$password);

$sql="INSERT INTO customer (FirstName, Surname, EmailAddress)
VALUES
('$_POST[firstname]','$_POST[surname]','$_POST[email]')";
$a=mysql_query($sql);

if (!$a)
  {
 echo mysql_error();
  }
else
{
    echo"1 record added";
}
mysql_close($con);
}
?>

Solution 2:

Make sure your file has .php extension.

Make sure you have put your PHP code in <?php ... ?> tags.

Solution 3:

Your script:

$con = mysql_connect($server,$username,$password);
// Check connectionif (mysql_connect_errno())
  {
  echo"Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO customer (FirstName, Surname, EmailAddress)
VALUES
('$_POST[firstname]','$_POST[surname]','$_POST[email]')";

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

mysqli_close($con);

You wrote if (!mysqli_query($con,$sql)) but in $con, you're using mysql_connect, not mysqli_connect,

So, it'll be:

if (!mysql_query($con,$sql)) {
      die('Error: ' . mysql_error($con));
}
        echo"1 record added";

    mysql_close($con);

Solution 4:

You created object of MySql like this

$con = mysql_connect($server,$username,$password);

and you use mysqli_qurey at below like

if (!mysqli_query($con,$sql))

How it possible??

Solution 5:

your password to local database must be empty and you should keep both the files in htdocs on xammp (form.html and contact_insert.php) in the same folder.

Post a Comment for "Html Form To Input Data Into Mysql Database"