将数据输入到mysql数据库中。

时间:2021-12-21 16:52:55

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.

我试着提交我的html表单,然后把数据保存到mysql数据库中。但是,一旦我点击提交按钮,它就会把我带到我的浏览器中显示的php代码。

Why is it doing this?

为什么会这样?

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

我在我的环境中使用XAMP,当我检查数据库时也没有添加任何数据。

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 个解决方案

#1


2  

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

您可以使用此代码。不要使用mysql和mysqli组合。

  <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);
}
?> 

#2


0  

Make sure your file has .php extension.

确保文件有.php扩展名。

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

确保您已经将PHP代码放在 标记。

#3


0  

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,

如果(!mysqli_query($con,$sql)),但是在$con中,您使用的是mysql_connect,而不是mysqli_connect,

So, it'll be:

所以,这将是:

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

    mysql_close($con);

#4


0  

You created object of MySql like this

你创建了MySql这样的对象。

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

and you use mysqli_qurey at below like

你可以在下面使用mysqli_qurey。

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

How it possible??

它如何可能? ?

#5


0  

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.

您的本地数据库密码必须是空的,并且您应该在xammp(窗体)上同时保存两个文件。html和contact_insert.php)在同一个文件夹中。

#6


0  

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);

?>

#1


2  

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

您可以使用此代码。不要使用mysql和mysqli组合。

  <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);
}
?> 

#2


0  

Make sure your file has .php extension.

确保文件有.php扩展名。

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

确保您已经将PHP代码放在 标记。

#3


0  

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,

如果(!mysqli_query($con,$sql)),但是在$con中,您使用的是mysql_connect,而不是mysqli_connect,

So, it'll be:

所以,这将是:

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

    mysql_close($con);

#4


0  

You created object of MySql like this

你创建了MySql这样的对象。

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

and you use mysqli_qurey at below like

你可以在下面使用mysqli_qurey。

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

How it possible??

它如何可能? ?

#5


0  

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.

您的本地数据库密码必须是空的,并且您应该在xammp(窗体)上同时保存两个文件。html和contact_insert.php)在同一个文件夹中。

#6


0  

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);

?>