html form to input data into mysql database

2019-09-01 02:36发布

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 browser.

Why is it doing this?

Im using XAMP for my environment, also when i check the database no data gets added either.

Any help would be greatly appreciated.

//html form
<div id="contact_form">
<form action="contact_insert.php"     method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="surname">
Email: <input type="text" name="email">
<input type="submit">
</form>
</div> 


//php contact_insert.php page
<?php

$username="root";
$password="password";
$server="127.0.0.1";
$database="eddiesdb";

$con = mysql_connect($server,$username,$password);
// Check connection
if (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);

?> 

6条回答
地球回转人心会变
2楼-- · 2019-09-01 03:11

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

  <div id="contact_form">
    <form action="contact_insert.php" method="post">
    Firstname: <input type="text" name="firstname">
    Lastname: <input type="text" name="surname">
    Email: <input type="text" name="email">
    <input type="submit" name="submit">
    </form>
    </div> 

<?php
    if(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);
}
?> 
查看更多
萌系小妹纸
3楼-- · 2019-09-01 03:11

Make sure your file has .php extension.

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

查看更多
太酷不给撩
4楼-- · 2019-09-01 03:13

Your script:

$con = mysql_connect($server,$username,$password);
// Check connection
if (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);
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-09-01 03:13

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??

查看更多
别忘想泡老子
6楼-- · 2019-09-01 03:20

Your script

<?php

$username="root";
$password="password";
$server="127.0.0.1";
$database="dbname";

$con = mysql_connect($server,$username,$password);
mysql_select_db("dbname");
// Check connection
if (mysql_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$fname = $_POST['firstname'];
$sname = $_POST['surname'];
$email = $_POST['email'];

$sql="INSERT INTO customer (FirstName, Surname, EmailAddress) VALUES ('".$fname."','".$sname."','".$email."')";

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

mysql_close($con);

?>
查看更多
欢心
7楼-- · 2019-09-01 03:31

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.

查看更多
登录 后发表回答