Tutorial Saving Data by Using PHP

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to save data with PHP
  • You will learn store data
  • You will learn how data with html extension, text, PHP

Requirements

  • You have basic about PHP
  • You have basic about HTML
  • You have basic about JavaScript
  • You must install XAMPP as a function running php or html by calling localhost in web address
  • Text Editor and a Browser

Difficulty

  • Basic

Tutorial Contents

Data or information we usually store into a database or use microsoft office, but this time different from the usual we save the file using extension html, txt, php and so forth.

This step is very easy and not too complicated for you to follow because only a few steps we will be able to see the results we do, if you are curious below there are some explanations how to make it

The first step we create a file form first

  • Open your Text Editor Software (Notepad ++, Sublime Text, Atom) in this tutorial I use Notepad++
  • Create new file and save as html extension. For ex database.html
<h2>> Menyimpan Data dengan cara Menggunakan PHP </h2>
<table>
<form method=&quot;post&quot; action=&quot;&quot;>>
;tr><td>Nama</td><td><input type=&quot;></td></tr>
    <tr><td>Isi</td><td><text&quot; name=&quot;nama&quot; style=&quot;width:300px"></td></tr>
    <tr><td>Isi</td><td><textarea name=&quot;data&quot; style=&quot;width:300px&quot><t;</textarea></td></tr>
    <tr><td></td><td><input type=&quot;submit&quot; name=&quot;ok&quot; value=&quot;Simpan"></td></tr>
</form>
</table>
  • Sample Results of Experiment

3.png

  • Next code php to process and store data into the file
<?php
if(isset($_POST['ok'])){
    if(empty($_POST['nama']) || empty($_POST['data'])){
        print "Lengkapi form";
    }else{
        $nama=$_POST['nama'];
        $data=$_POST['data'];
        $tanggal=date("h:i:s");
        $buka=fopen("hasil.html",'a');
        fwrite($buka,"nama : ${nama} <br> ");
        fwrite($buka,"dibuat : ${tanggal} <br> ");
        fwrite($buka," isi : ${data} <br> ");
        fwrite($buka,"<hr>");
        fclose($buka);
        print "data berhasil disimpan";
    }

fopen() function opens the file Then follows the mode parameter "a" , this mode parameter opens the file and puts the pointer at the end of the file, if the file does not exist then it will be created first.

so the file "hasil.html"if nothing will be created first, and if it exists then the data will be added after the previous data.

there are many fopen () mode options, as shown in the following table.

A list of possible modes for fopen() using mode

modeDescription
'r'Open for reading only; place the file pointer at the beginning of the file.
'r+'Open for reading and writing; place the file pointer at the beginning of the file.
'w'Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+'Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a'Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
'a+'Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
'x'Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL_O_CREAT flags for the underlying open(2) system call.
'x+'Create and open for reading and writing; otherwise it has the same behavior as 'x'.
'c'Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).
'c+'Open the file for reading and writing; otherwise it has the same behavior as 'c'.
e'Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems.

source

Then fwrite () works to write something into the file.

Then close () works to close the file again.

Please try and see the results by opening the file "database.html "
2.png

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Tutorial is very basic and similar content has been shared many times, does not meet utopian standards.

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