将用户输入从文本框插入数据库(使用mysql从PHP插入到PHPMYADMIN)

将用户输入从文本框插入数据库(使用mysql从PHP插入到PHPMYADMIN),php,html,mysql,database,Php,Html,Mysql,Database,你们如何使用mysql将用户输入从文本框(html/php)插入数据库(phpmyadmin) 我不断收到错误“插入失败”是不是我的代码丢失了什么 我确实在网上搜索了如何修复它,但没有任何效果。我认为我的代码中缺少了一些东西,我无法将其锁定 下面的所有文件都在一个名为index.php的php文件中 <!DOCTYPE html> <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $db = 'dad_

你们如何使用mysql将用户输入从文本框(html/php)插入数据库(phpmyadmin)

我不断收到错误“插入失败”是不是我的代码丢失了什么

我确实在网上搜索了如何修复它,但没有任何效果。我认为我的代码中缺少了一些东西,我无法将其锁定

下面的所有文件都在一个名为index.php的php文件中

<!DOCTYPE html>
 <?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'dad_trading';

$dbconn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db);




if (isset($_POST['submit']))
{
    $Lastname   = $_POST['LastName'];
    $firstname  = $_POST['FirstName'];
    $Middlename = $_POST['MiddleName'];
    $address    = $_POST['Address'];
    $city       = $_POST['City'];
    $zipcode    = $_POST['ZipCode'];
    $email      = $_POST['email'];
    $number     = $_POST['number'];


     $query = ("INSERT INTO customer ([LName], [FName], [MName], [Street], [City], [ZipCode], [Email], [ContactNo]) VALUES ('$Lastname', '$firstname', '$Middlename', '$address', '$city','$zipcode', '$email', '$number')");

if(mysql_query($query))
 {
echo "<script>alert('INSERTED SUCCESSFULLY');</script>";
}
else
 {
 echo "<script>alert('FAILED TO INSERT');</script>";
 }

 }
?>

<html>
    <head>
        <meta charset="UTF-8">
        <title>sample</title>
    </head>
    <body>
        <form action="" method = "POST">

   First name:   
  &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  Middle Name:
  &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  Last Name:<br>
  <input name="FirstName" size="15" style="height: 19px;"  type="text" required>
      &nbsp; &nbsp; &nbsp; 
  <input name="MiddleName" size="15" style="height: 19px;"  type="text" required>
      &nbsp; &nbsp; &nbsp; 
  <input name="LastName" size="15" style="height: 19px;"  type="text" required>

  <br><br>

    Email Address:<br>
  <input name="email"  type="text" required placeholder="Enter A Valid Email Address" style="height: 19px;" size="30"><br><br>

  Home Address: <br>
  <input name="Address" type="text" required placeholder="Enter your home Address" style="height: 19px;" size="30" maxlength="30"><br><br>

  City:
   &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
  Zipcode:
   <br>
  <input name="City" size="7" style="height: 19px;"  type="text" required>
    &nbsp; &nbsp; 
    <input name="ZipCode" size="7" style="height: 19px;"  type="text" required>
    <br><br>

  Telephone/Mobile Number: <br>
  <input name="number" type="text" required id="number" placeholder="Mobile Number" style="height: 19px;">

<br>
<br>

<button type ="submit" name="submit" value="send to database"> SEND TO DATABASE </button>
</form>
    </body>
</html>   


尝试使用服务器变量添加表单操作

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

尝试使用服务器变量添加表单操作

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

下面是一个有效的代码示例
mysql\u connect
已被弃用,
newmysqli
有效

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

下面是一个有效的代码示例
mysql\u connect
已被弃用,
newmysqli
有效

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
我为自己的目的修复了OP,它对我很有效。目标是从web表单创建数据库条目以进行一些测试。
样品
我为自己的目的修复了OP,它对我有效。目标是从web表单创建数据库条目以进行一些测试。
样品

从字段名中删除方括号,然后重试可能会有帮助好的,我会尝试的now@AmitChauhan它起作用了。。哈哈,我真傻。我被一个简单的错误弄得筋疲力尽从字段名中删除方括号并尝试可能会有所帮助好的,我会尝试的now@AmitChauhan它起作用了。。哈哈,我真傻。我因为一个简单的错误而感到压力很大。你的第一句话很管用。textbox中的数据现在被插入到数据库中。此外,您必须将服务器变量放入操作中,并且您的Welcome您的第一条注释有效。textbox中的数据现在被插入到数据库中。此外,您还必须将服务器变量和Welcome放入操作中。这是一段很长的代码,没有任何解释。如果您添加一些解释,说明为什么您的代码是一个好的答案,并帮助读者理解,这将有助于读者。只有代码的回答被认为是不礼貌的。这是一段很长的代码,没有解释。如果您添加一些解释,说明为什么您的代码是一个好的答案,并帮助读者理解,这将有助于读者。只有代码的回答被认为是不礼貌的。