html表单将数据输入到mysql数据库中 [英] html form to input data into mysql database

查看:1503
本文介绍了html表单将数据输入到mysql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



为什么要这样做?



我使用XAMP作为我的环境,当我检查数据库时也没有添加数据。



任何帮助将不胜感激。

  // html表单
< div id =contact_form>
< form action =contact_insert.phpmethod =post>
名字:< input type =textname =firstname>
姓氏:< input type =textname =surname>
电子邮件地址:< input type =textname =email>
< input type =submit>
< / form>
< / div>


// php contact_insert.php页面
<?php

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

$ con = mysql_connect($ server,$ username,$ password);
//检查连接
if(mysql_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();

$ b $ sql =插入客户(名字,姓氏,电子邮件地址)
VALUES
('$ _POST [firstname]','$ _ POST [姓氏], '$ _ POST [邮件]');

if(!mysqli_query($ con,$ sql))
{
die('Error:'。mysqli_error($ con));
}
回声添加1条记录;

mysqli_close($ con);

?>


解决方案

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

 < div id =contact_form> 
< form action =contact_insert.phpmethod =post>
名字:< input type =textname =firstname>
姓氏:< input type =textname =surname>
电子邮件地址:< input type =textname =email>
< input type =submitname =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 =插入客户(名字,姓氏,电子邮件地址)
VALUES
('$ _POST [firstname]','$ _ POST [surname]','$ _POST [电子邮件]');
$ a = mysql_query($ sql);

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


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

?> 

解决方案

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

这篇关于html表单将数据输入到mysql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆