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

时间:2022-01-22 07:39:36

How do you guys insert user input from textbox(html/php) to database (phpmyadmin) using mysql

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

I keep getting the error "failed to insert" is there something missing with my code.

我一直收到错误“无法插入”我的代码中缺少一些东西。

I did search online on how to fix it but nothing is working. I think something is missing with my code and I can't pin point it.

我在网上搜索了如何解决它,但没有任何工作。我认为我的代码缺少一些东西,我无法指出它。

all files below are in 1 php file named index.php

以下所有文件都在1个名为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>   

2 个解决方案

#1


0  

try add the form action using server variable

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

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

#2


0  

Here's an example of code that works. From w3Schools. mysql_connect is deprecated, new mysqli works.

这是一个有效的代码示例。来自w3Schools。不推荐使用mysql_connect,新的mysqli可以使用。

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

Having tried myself to make your code work and as a beginner not knowing it was broken, I came across a few issues using your code. I leave this out for anyone who could end up here while learning on how to insert data in a database and to anyone who could want to point out the mistakes OP made by editing this answer.

在尝试使自己的代码工作之后,作为初学者而不知道它已被破坏,我在使用您的代码时遇到了一些问题。我将这个问题留给那些可以在学习如何在数据库中插入数据的人以及任何想要通过编辑此答案来指出OP所犯错误的人。

#1


0  

try add the form action using server variable

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

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

#2


0  

Here's an example of code that works. From w3Schools. mysql_connect is deprecated, new mysqli works.

这是一个有效的代码示例。来自w3Schools。不推荐使用mysql_connect,新的mysqli可以使用。

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

Having tried myself to make your code work and as a beginner not knowing it was broken, I came across a few issues using your code. I leave this out for anyone who could end up here while learning on how to insert data in a database and to anyone who could want to point out the mistakes OP made by editing this answer.

在尝试使自己的代码工作之后,作为初学者而不知道它已被破坏,我在使用您的代码时遇到了一些问题。我将这个问题留给那些可以在学习如何在数据库中插入数据的人以及任何想要通过编辑此答案来指出OP所犯错误的人。