TUTORIAL || How to make Crud with php mysql using Edit data method Part #3

in #utopian-io6 years ago (edited)

What Will I Learn?

  • We will learn how to make crud
  • We will learn how to edit and update data with crud method
  • We will learn systematic work of crud program with method of editing and updating data

Requirements

  • Xampp
  • Sublime text (text editor)
  • Browser application

Difficulty

  • Intermediate

Tutorial Contents

C (Create): which means to create a new data, for example we are doing the registration disebuah web that already constitute Create from CRUD because we make and save registration data to database.

R (Read): Read or display a data that was located in MySQL database for example, then displayed in WEB using Php

U programming language (Update): well for this one process is editing a data from database which then edited using Php programming language in the form of WEB. Example edit facebook profile.

D (Delete) : Surely you know what function is not it? Its function is almost the same as Update but this process is to do the deletion of data in the database through Php language. Examples on a blog sometimes have comments, then we delete the comment, well it includes the delete process in CRUD.

CRUD is often used in data processing applications that most mengguanakan CRUD functions therein. This function is used to add data, delete data, and update data.

Edit: Activity modifies the format of an output or input by inserting or deleting characters.

Getting Started
  • Create a data base with the name "crud".

1.png

  • then create table with "user"

2.png

  • And then create 4 colom in User Table (id, nama, alamat, pekerjaan)

3.png

  • then create the main index.php file

index.php

"
< !DOCTYPE html>

< html>

< head>

< title>How To Make CRUD With PHP And MySQL - Displays data from database< /title>

< link rel="stylesheet" type="text/css" href="style.css">

< /head>

< body>
< div class="judul">

    < h1>Make CRUD With PHP And MySQL< /h1>

    < h2>Displays data from database< /h2>
    
< /div>
< br/>

< ?php 
if(isset($_GET['pesan'])){
    $pesan = $_GET['pesan'];
    if($pesan == "input"){
        echo "Data berhasil di input.";
    }else if($pesan == "update"){
        echo "Data berhasil di update.";
    }else if($pesan == "hapus"){
        echo "Data berhasil di hapus.";
    }
}
? >
< br/>
< a class="tombol" href="input.php">+ Tambah Data Baru< /a>

< h3>Data user< /h3>
< table border="1" class="table">
    < tr>
        < th>No< /th>
        < th>Nama< /th>
        < th>Alamat< /th>
        < th>Pekerjaan< /th>
        < th>Opsi< /th>     
    </tr>
    <?php 
    include "koneksi.php";
    $query_mysql = mysql_query("SELECT * FROM user")or die(mysql_error());
    $nomor = 1;
    while($data = mysql_fetch_array($query_mysql)){
    ? >
    < tr>
        < td><?php echo $nomor++; ?>< /td>
        < td><?php echo $data['nama']; ?>< /td>
        < td><?php echo $data['alamat']; ?>< /td>
        < td><?php echo $data['pekerjaan']; ?>< /td>
        < td>
            < a class="edit" href="edit.php?id=<?php echo $data['id']; ?>">Edit</a> |
            < a class="hapus" href="hapus.php?id=<?php echo $data['id']; ?>">Hapus</a>                  
        < /td>
    < /tr>
    < ?php } ?>
< /table>

< /body>
< /html>
"

  • after that create an index.php connection with the database.

koneksi.php

"
< ?php
$host = mysql_connect("localhost","root","");

$db = mysql_select_db("crud");

? >
"

  • We create attack from edit to edit the contents of the database and we will create a file with edit.php edit name.

edit.php

"
< !DOCTYPE html>

< html>

< head>
< title>How To Make CRUD With PHP And MySQL - Displays data from database

< /head>

< body>
< div class="judul">

    < h1>Make CRUD With PHP And MySQL</h1>

    < h2>Displays data from database</h2>
</div>

<br/>

<a href="index.php">Lihat Semua Data</a>

<br/>
<h3>Edit data</h3>

<?php 
include "koneksi.php";
$id = $_GET['id'];
$query_mysql = mysql_query("SELECT * FROM user WHERE id='$id'")or die(mysql_error());
$nomor = 1;
while($data = mysql_fetch_array($query_mysql)){
?>
<form action="update.php" method="post">        
    <table>
        <tr>
            <td>Nama</td>
            <td>
                <input type="hidden" name="id" value="<?php echo $data['id'] ?>">
                <input type="text" name="nama" value="<?php echo $data['nama'] ?>">
            </td>                   
        </tr>   
        <tr>
            <td>Alamat</td>
            <td><input type="text" name="alamat" value="<?php echo $data['alamat'] ?>"></td>                    
        </tr>   
        <tr>
            <td>Pekerjaan</td>
            <td><input type="text" name="pekerjaan" value="<?php echo $data['pekerjaan'] ?>">< /td>                 
        < /tr>  
        < tr>
            < td></td>
            < td><input type="submit" value="Simpan">< /td>                 
        < /tr>              
    < /table>
< /form>
< ?php } ?>

< /body>

< /html>
"

-Then make that will be diverted from edit.php for the database can be in updatae

  • Now we create update.php file

update.php

"

<?php

include 'koneksi.php';

$id = $_POST['id'];

$nama = $_POST['nama'];

$alamat = $_POST['alamat'];

$pekerjaan = $_POST['pekerjaan'];

mysql_query("UPDATE user SET nama='$nama', alamat='$alamat', pekerjaan='$pekerjaan' WHERE id='$id'");

header("location:index.php?pesan=update");
?>
"

Program description

"
< ?php

include "koneksi.php";

$id = $_GET['id'];

$query_mysql = mysql_query("SELECT * FROM user WHERE id='$id'")or die(mysql_error());

$nomor = 1;

while($data = mysql_fetch_array($query_mysql)){

? >
"

  • Try to note the syntax above, the syntax above we will connect the php database mysql.

"
include "koneksi.php";
"

  • Then syntax above to enter the id which is sent via url

"
$id = $_GET['id'];
"

  • And then to display data from database according to id received.

"
$query_mysql = mysql_query("SELECT * FROM user WHERE id='$id'")or die(mysql_error());
"

  • In mysql_query syntax above as I previously explained that mysql_query, is a useful php function to run mysql command. So here to display user data that id in accordance with the id that was sent earlier.

"
< form action="update.php" method="post">
"

  • To send the data id that is being edited. so that later in the update.php file find out the data which id which will be changed.

"
< input type="hidden" name="id" value="< ?php echo $data['id'] ? >" >
"

  • So when submitted the page will be diverted to update.php therefore we must create update.php file.

On update.php

"
$id = $_POST['id'];

$nama = $_POST['nama'];

$alamat = $_POST['alamat'];

$pekerjaan = $_POST['pekerjaan'];
"

  • Next we will update the data with mysql command as well. Use mysql_query () to run mysql command to update the data. So here we will update the data id in accordance with the id sent.

"
mysql_query("UPDATE user SET nama='$nama', alamat='$alamat', pekerjaan='$pekerjaan' WHERE id='$id'");
"

  • Finally switch his page to index.php back at once by filling the update message so that in index.php can we show also notification message data successfully updated.

"
header("location:index.php?pesan=update");
"

  • after that to beautify from it with css

style.css

"
body{
font-family: 'roboto';
color: #000;
}

.judul{
background: #42A5F5;
padding: 10px;
text-align: center;

}

.judul h1,h2,h3{
height: 15px;
}

a{
/color: #fff;/
padding: 5px;
text-decoration: none;
}

.table{
border-collapse: collapse;
}

table.table th th , table.table tr td{
padding: 10px 20px ;
}
"

  • if all the scarcity is over and understand how the work of crud with php mysql using data edit method, now we run the program it will come out seprti this and then select edit.

1.png

  • then it will look like this and then change the data then press update button.

2.png

  • then the index will look like this with the data in the changed user table.

3.png

  • and finish crud by using data edit method

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @rajamalikulfajar, your contribution was rejected by the supervisor @espoem because he found out that it did not follow the Utopian rules.

Upvote this comment to help Utopian grow its power and help other Open Source contributions like this one. Do you want to chat? Join me on Discord.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Rejected. The code is from http://dutoro.blogspot.cz/2017/07/membuat-form-input-edit-dan-delete.html

  • The contribution is not formatted well.
  • There is missing the tutoring part, lots of information is repeated across the parts. If they wanted to contribute, this should have been put together if they actually submitted original content.

You can contact us on Discord.
[utopian-moderator]

Nice tutorial on CRUD. There is another method to make CRUD in PHP, which is using prepared statement. This method is used to execute same query multiple times with high efficiency. Source: Simple crud in PHP and MySQL

Coin Marketplace

STEEM 0.31
TRX 0.11
JST 0.034
BTC 64332.82
ETH 3146.25
USDT 1.00
SBD 4.17