how to insert multiple data into the database simultaneously

in #utopian-io6 years ago (edited)

What Will I Learn?

Write here briefly the details of what the user is going to learn in a bullet list.

  • You will learn about database mysql
  • You will learn about insert mutiple record
  • You will learn about connect php with mysql

Requirements

Write here a bullet list of the requirements for the user in order to follow this tutorial.

  • you have basic php
  • you have basic mysql

Difficulty

  • Basic

Tutorial Contents

entering the data simultaneously is needed in making a good database, with the existence of this program user or admin can enter the required data faster and save time than to enter data one by one. To find out more about this program, consider the steps that will be described below.

  • The first stage in making this form is to create a database, coding for the basenya data can be seen below if you do not already have it:
create database inputdata;
use inputdata;
create table biodata(
    no_ktp int(16) not null primary key,
    firstname varchar(15) not null,
    lastname varchar(15) not null,
    address varchar(40));

image.png

  • the next step is to call hosting, username, password and database used
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inputdata";
  • Furthermore the program input to connect with the database, can be seen below.
$conn = mysqli_connect($servername, $username, $password, $dbname);
  • Create a condition where if the database is not connected, a notification will appear.
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
  • Next is the main purpose of this program is to input some data at once into the database. Coding can be seen below.
$sql = "INSERT INTO biodata (no_ktp,firstname, lastname, address)
VALUES ('1021032265680001','sofyan', 'sauri','jln.medan- banda aceh desa bayam' );";
$sql .= "INSERT INTO biodata (no_ktp,firstname, lastname, address)
VALUES (' 1021546478980001','danil', 'anderson', 'jln pattimura desa pedang');";
$sql .= "INSERT INTO biodata (no_ktp,firstname, lastname, address)
VALUES ('1028708091210801','mando', 'Doreh', 'jln.teuku umar desa pante ')";

To input some data at once also needed some query to enter it into database.

  • make some conditions where, if the data entered successfully, it will appear notification succes while if the data is not successfully entered, then there is an error or data entered not in accordance with the tip of the variable used.
if (mysqli_multi_query($conn, $sql)) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
  • In the last stage input a command to close the program, the coding can be seen below.
mysqli_close($conn);
?>



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.