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

how to insert data into mysql db using form in php database
how to create an html form that stores data in a mysql database using php part 1 of 4
how to insert data in phpmyadmin database from html form
how to store form data in database using php
how to insert data in mysql using php w3schools
how to save data in database on button click in php
php submit form to database and email
how to insert data dynamically in mysql using php

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

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

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

try add the form action using server variable

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

An example to insert some data in to the MySQL database using PHP, To use a PHP script on your web page, you just need to end the file name with . php and Firstname: <input type="text" name="fname" /><br><br> in the textboxes can then be saved to the database in the “insert.php” page. Go to http://mysql.cis.ksu.edu/phpmyadmin and type your MySQL ID and password being given. Insert user input from textbox to Database (PHP to PHPMYADMIN using mysql) Code not working for some reason Browse other questions tagged php mysql database forms

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

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

Insert a Record in Mysql Database Using a Textbox(Part-1) in PHP , Phpmyadmin insert In database.insert in php Hindi/urdu tutorial. our mysql database using Duration: 9:19 Posted: Apr 10, 2017 PHP Code to INSERT Data Into MySQL Database. There are two methods you can use to INSERT data into your MySQL database. The PHP MySQLi method and PHP Data Object or PDO method. MySQLi Method. First, you’ll need to establish a connection to a database. After that is done, we can proceed with the MySQL query INSERT. Here is a full PHP code

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>   

How would I be able to insert data to a database using a textbox in a , Basically I have created a form with several text boxes First Name, Surname, Username and Password. be able to insert the data typed into the text boxes into my database using PHP, Here is the send.php which will handle input from the form. 0) { echo 'That username already exists in the database. How to submit multiple textbox data directly inside MySQL database table in php. In this tutorial we are creating multiple input textbox to get multiple data from user. For example name, fathers name, address etc.

How to Use PHP to Insert Data Into MySQL Database, In this tutorial you will learn how to use PHP to insert data into MySQL database using PDO or MySQLi. phpMyAdmin option in your hosting database That way, each column represents a specific value. Server (where I have set up Apache and built a Website, which for now I prefer not to write in the textbox below). When a user clicks the submit button of the add record HTML form, in the example above, the form data is sent to 'insert.php' file. The 'insert.php' file connects to the MySQL database server, retrieves forms fields using the PHP $_REQUEST variables and finally execute the insert query to add the records. Here is the complete code of our

Insert Data From a Form Into a Database using php mysqli, Insert Data From a Form Into a Database using php mysqli. Now we will Firstname: <input type="text" name="firstname"> When a user clicks the submit button in the HTML form, in the example above, the form data is sent to "insert. php". echo "Failed to connect to MySQL: " . mysqli_connect_error(); } As at my end it is working fine, you just need to download the code , extract the file in your local server file , go to your Phpmyadmin import db.sql file and then open the program , using syntax : localhost/insert.php

Insert Data in Database Using PHP, After reading you will learn inserting data into database using PHP. You just need to import it in your phpmyadmin it will create the required tables. Now fill the form fields and submit it, data will be saved to MySql database and a for hiding values in URL--> <h2>Form</h2> <label>Name:</label> <input class=" input"� Insert Data Into MySQL Using MySQLi and PDO. After a database and a table have been created, we can start adding data in them. Here are some syntax rules to follow: The SQL query must be quoted in PHP; String values inside the SQL query must be quoted; Numeric values must not be quoted; The word NULL must not be quoted

Comments
  • remove square bracket from field name and try may be help
  • okay I'll try it now
  • @AmitChauhan it work.. HAHA silly me. I am stressed out with a simple error
  • Your first comment works. data from textbox is now being inserted to the database..
  • also you have to put server variable in action and your welcome
  • That's a very long piece of code with no explanation. It would assist readers if you added some explanation why your code is a good answer and help them understand. Code only answers are considered impolite.