简体   繁体   English

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

[英]Insert user input from textbox to Database (PHP to PHPMYADMIN using mysql)

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>   

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

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

Here's an example of code that works. 这是一个有效的代码示例。 From w3Schools . 来自w3Schools mysql_connect is deprecated, new mysqli works. 不建议使用mysql_connect可以使用new 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. 对于那些可能会在这里学习如何在数据库中插入数据的人以及想要指出编辑此答案所导致的操作错误的人,我将予以忽略。

I fixed the OP for my purposes, and it worked for me. Goal was to create database entries from a web form for some testing.

<html>
        <head>
            <meta charset="UTF-8">
            <title>sample</title>
        </head>

        <?php
    //These $variables related to the form data html elements eg "<input 
    //name="City"" input name=values, case sensitive which are derived from 
    //the //form submit with POST type, from the form at the end of this code 
    //block.
        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'];

    //This is the sql query to apply the form inpur field values into the 
    database //from the user form in the web page. There is no validation 
    checking, which //an example at TutorialRepublic for CRUD and php...:

    https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud- 
   application.php

    //...Is really much more thorough.

            $con = mysqli_connect('localhost','root','Levaral','test');
            $query = "INSERT INTO customer (LastName, FirstName, MiddleName, 
      Address, City, Zipcode, email, number) VALUES (" . " '" . $Lastname . 
    "', '" . $firstname . "', '" . $Middlename . "', '" . $address . "', '" . 
    $city . "', '" . $zipcode . "', '" . $email . "', '" . $number . "')";
            if (mysqli_query($con,$query))
                {
                echo "<script>alert('INSERTED SUCCESSFULLY');</script>";
                }
                else
                {
                echo "<script>alert('FAILED TO INSERT');</script>";
                }

                }
                ?>


    <body>
    //put html element data in a <form> so you can send the data here by POST 
    //type, this stumped me 
    //at first when I was starting.
    //I guess since the form is in the same page, it is available to the PHP 
    //function as some default.

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

    //This part below was just for my feedback to see if it worked by 
    //returning some //data from the query, as in progress to have an edit 
    //area 
    //on the same page //without affecting the original if my mind serves me 
    //right.

        <?php
        /////

        mysqli_select_db($con,"customer");
        $sql="SELECT * FROM customer WHERE FirstName = '".$firstname."'";
        $result = mysqli_query($con,$sql);

        echo "<table>
        <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>Email</th>
        <th>Number</th>
        </tr>";
        while($row = mysqli_fetch_array($result)) {
        echo '<tr style="width:20%">';
            echo '<td style="width:20%">' . $row['FirstName'] . "</td>";
            echo '<td style="width:20%">' . $row['LastName'] . "</td>";
            echo '<td style="width:20%">' . $row['City'] . "</td>";
            echo '<td style="width:20%">' . $row['email'] . "</td>";
            echo '<td style="width:20%">' . $row['number'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        mysqli_close($con);
        ?>

    </body>
    </html>   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将用户输入从文本框插入数据库(使用mysql从PHP到PHPMYADMIN)代码由于某些原因不起作用 - Insert user input from textbox to Database (PHP to PHPMYADMIN using mysql) Code not working for some reason 使用phpMyAdmin将用户添加到php中的MySQL数据库 - Adding user to MySQL database in php using phpMyAdmin PHP,MYSQL,phpmyadmin-使用html表单将记录输入到数据库 - PHP, MYSQL, phpmyadmin - Using a html form to input records to a database 使用phpmyadmin通过phpmyadmin登录到数据库的用户? - Log-in user to his database using php to mysql via phpmyadmin? 无法使用phpmyadmin从html表单将行插入mysql数据库 - Cannot insert rows into mysql database from an html form using phpmyadmin mysql phpmyadmin用户输入COMPARE数据库 - mysql phpmyadmin User Input COMPARE database 尝试使用php将数据从xml导入phpmyadmin中的mysql数据库 - Trying to importing the data from xml using php to mysql database in phpmyadmin PHP / MySQL:从用户输入创建数据库 - PHP / MySQL: Create Database from User Input 使用文本框mysql php从mysql数据库中删除数字 - Take away number from mysql database using textbox mysql php 如何使用php和html从表单输入框中检查用户的输入对应于mysql中数据库的结果 - How to check the input of a user from a form input box using php and html corresponds to a result from the database in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM