2 form satu data mysql

misi master, bagaiman cara menambah data pada dua tabel sekaligus? Disini saya punya tabel a dan tabel b, Saya ingin dalam satu form tambah data akan disimpan kedalam dua tabel dengan isi yg sama. Saya sudah pakai cara yg didapat dari google, tapi hasilnya cuma menambah pada tabel_a. Mohon pencerahannya

2 form satu data mysql
2 form satu data mysql

@candra1098

41 Kontribusi 10 Poin

Dipost: 6 tahun yang lalu Update 2 tahun yang lalu


Jawaban

coba tulis kaya gini gan

$query = "INSERT INTO tabel_a (nama, umur, alamat) VALUE ('$nama', '$umur', '$alamat');";
$query .= "INSERT INTO tabel_b (nama, umur, alamat) VALUE ('$nama', '$umur', '$alamat')";

$result = mysqli_multi_query($link, $query);

semoga berhasil gan....

@maddock

119 Kontribusi 115 Poin

Dipost: 6 tahun yang lalu Update 2 tahun yang lalu


Mungkin bisa coba dengan cara seperti ini gan

 function tambahData ($nama, $umur, $alamat) {
      global $link;
      $query = "INSERT INTO tabel_a (nama, umur, alamat) OUTPUT INSERTED.'$nama', '$umur','$alamat' 
      INTO tabel_b (nama, umur, alamat) VALUES ('$nama', '$umur','$alamat')";
      $result =  mysql_query($link,$query) or die ('gagal tambah');
      
      return $result;

Semoga berhasil , CMIIWIW ^_^

@chelvin

36 Kontribusi 18 Poin

Dipost: 6 tahun yang lalu Update 3 tahun yang lalu


thanks sudah bisa..

@candra1098

41 Kontribusi 10 Poin

Dipost: 6 tahun yang lalu Update 4 tahun yang lalu


@chelvin kasusku sama tapi agak beda. jika query tabel_a itu update lalu query tabel_b insert bisakah? jadi update tabel_a lalu otomatis kesimpen / insert di tabel_b.. mohon pencerahannya .

@muhazzam

1 Kontribusi 0 Poin

Dipost: 4 tahun yang lalu Update 2 tahun yang lalu


kalo untuk bahasa java netbeans bisa ga? minta contoh codingannya dong

@ibeh

3 Kontribusi 0 Poin

Dipost: 3 tahun yang lalu


saya punya kasus sebelumnya sebelum di relasikan untuk insert itu bisa tapi setelah direlasikan malah tidak bisa malah eror

INSERT INTO `tbl_guru` (`id`, `id_jadwal`, `id_matapelajaran`, `id_kelas`, `nama`, `alamat`, `jenis_kelamin`, `agama`, `tempat_lahir`, `tanggal_lahir`) VALUES (NULL,'$id_jadwal','$id_matapelajaran','$id-kelas','$nama','$alamat','$jk','$agama','$tl','$tr')

@zidanabd

3 Kontribusi 0 Poin

Dipost: 2 tahun yang lalu Update 2 tahun yang lalu

  • ada baiknya buka pertanyaan baru gan, jangan ditumpuk.. hehehe - @uzzielpelawak


Login untuk gabung berdiskusi

A step by step guide to create a very basic dynamic web page. I am using Ubuntu 16.04 machine.

This article in a nutshell:

  1. Download prerequisites

2. Start the web server

3. Create a new MySQL database with table and columns

4. Create an HTML form to collect data.

5. Create a PHP file to connect to the database that will accept the submitted form data and add it to the database.

6. Run the HTML form file on the web server, add user data and check if the database is populated.

First-up is the environment setup:

Code editor (A developer’s code editoris a personal choice. You can use any of your favourite. I have Visual Studio Code editor. It can be downloaded from here https://code.visualstudio.com/download)

Installing Apache 2 web-server on Ubuntu local machine via the terminal.

$ sudo apt-get update

$ sudo apt-get install apache2

Once installed, open your web browser and type localhost. It should open Apache web server information page. This means your web server is running now. Some additional commands to start, stop and restart the web server which will be needed later are:

$ sudo [add space]/etc/init.d/apache2 [add space] start

$ sudo [add space]/etc/init.d/apache2 [add space] stop

$ sudo [add space]/etc/init.d/apache2 [add space] restart

By default Apache2 web server runs all the files from /var/www/html folder located in computer .

2 form satu data mysql

Next we will create a new HTML and PHP file in this /var/www/html folder.

Installing MySQL with Apache web server

$ sudo apt get install mysql-server

Installing PHP with Apache web server

$ sudo apt install php libapache2-mod-php

Once PHP is installed, restart Apache2 web server using the restart command and create a test.php file. Open this file in the code editor and write the code.

$ sudo touch [space here] /var/www/html/test.php

2 form satu data mysql

Run this file in your browser’s localhost. It should display ‘Hello World’ indicating that PHP is working fine.

2 form satu data mysql

MySQL database

$ sudo -u root -p

mysql> show databases;

mysql> CREATE DATABASE home;

mysql> CREATE TABLE people ( name VARCHAR(20), email VARCHAR(20));

I forgot to add an ID so I used ALTER TABLE command

mysql>ALTER TABLE people ADD ID int NOT NULL;

mysql>ALTER TABLE people MODIFY ID int NOT NULL AUTO_INCREMENT;

To view what is created

mysql> desc info;

2 form satu data mysql

So the table is ready now its time to code a simple HTML form in the folder with path var/www/html/basicform.html . Open this file in the code editor and write the code.

HTML basic form

$ sudo touch [space here] /var/www/html/basicform.html

2 form satu data mysql

Run this file in your browser’s localhost. It should display the form.

2 form satu data mysql

Now its time to create a PHP file and add some PHP code. This code will enable the database to be populated by form data.

PHP Code

$ sudo touch [space here] /var/www/html/basicphp.php

2 form satu data mysql

make sure you add your username and password

Run the basicform.html file in the localhost, add the user data and hit submit.

2 form satu data mysql

Finally, check the database.

Check Populated Database

mysql> SELECT * FROM people;

2 form satu data mysql

Hope this has helped you.Please do let me know in the comments below. Thank you and Happy Coding.