<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<?php
   // phpinfo ();
   // session_start();
   @ini_set("display_errors","on");
   error_reporting (E_ALL);
?>

<html>

<head>
        <title> DB Example </title>

        <style type="text/css">

            table, td, th {
                background-color: #eaf2ff;
                padding: 2px 15px 2px 15px;
                border-collapse:collapse;
            }

            th {
                background-color: #95B8E7;
            }
            
            #menu li {
              background-color: #7fbfff;
              display: inline;
              padding: 10px 20px 10px 20px;
              margin: 20px; 
              border: dotted 1px gray; 
            }

            .error {
                margin: 0.5em auto 0.5em auto;
                padding: 15px 10px 15px 55px;
                width: 450px;
                color: #ff0000;
                background: #fff0f0;
                border: 1px solid #ffcccc;
            }
            
            .note {
                margin: 0.5em auto 0.5em auto;
                padding: 15px 10px 15px 55px;
                width: 450px;
                background: #fffddf;
                border: 1px solid #ffcc00;
            }
        </style>
</head>

<body>

<ul id="menu">
<li><a href="?fce=create"> create table </a></li>
<li><a href="?fce=insert"> insert item </a></li>
<li><a href="?fce=list"> list table </a> </li>
<li><a href="?fce=drop"> drop table </a></li>
</ul>

<br> <br>

<!-- bez mezery mezi </a> a </li> -->

<?php

/******************************* FUNCTIONS ********************************/

function err ($msg)
{
    echo '<div class="error">', "\n";
    echo "Error: $msg \n";
    echo '</div>', "\n";
    exit;
}

function note ($msg)
{
    echo '<div class="note">', "\n";
    echo "Note: $msg \n";
    echo '</div>', "\n";
}

/* quote string */

function sql_str ($connection, $value)
{
    return "'" . $connection->quote ($value) . "'";
}

function html_str ($value)
{
    return htmlspecialchars ($value);
}

/* parameters and session variables */

function param ($param_name)
{
    if (isset ($_POST[$param_name]))
    {
       $value = $_POST[$param_name];
    }
    else if (isset ($_GET[$param_name]))
    {
       $value = $_GET[$param_name];
    }
    else
    {
       $value = "";
    }

    // echo "$param_name: ", $value, '<br>', "\n";

    return trim ($value);
}

function recall_variable ($session_name)
{
    $value = "";
    if (isset ($_SESSION[$session_name]))
       $value = $_SESSION[$session_name];
    // echo "$session_name: ", $value, '<br>', "\n";
    return $value;
}

function set_variable ($session_name, $value)
{
    $_SESSION[$session_name] = $value;
    return $value;
}

/********************************* START **********************************/

/* database parameters */

$db_host = "localhost";
$db_user = "uzivatel";
$db_password = "heslo";
$database = "mydb";

/* database connection */

try
{
   $connection = new PDO ("mysql:host=$db_host;dbname=$database", $db_user, $db_password);
}
catch(PDOException $e)
{
   Err ($e->getMessage());
}

/* actions */

$fce = param ("fce");

if ($fce == "create")
{
   $cmd = "
   CREATE TABLE colors
   (
           name VARCHAR(80) NOT NULL PRIMARY KEY, 
           red INT,
           green INT,
           blue INT
   )
   ";
   try
   {
      $connection->query ($cmd);
   }
   catch(PDOException $e)
   {
      Err ($e->getMessage());
   }
}

if ($fce == "drop")
{
   $cmd = "DROP TABLE colors";
   try
   {
      $connection->query ($cmd);
   }
   catch(PDOException $e)
   {
      Err ($e->getMessage());
   }
}

if ($fce == "insert")
{
   $cmd = "
   INSERT INTO colors VALUES
   ('blede modra', 128, 128, 255)
   ";
   $connection->query ($cmd);
   
   $fce = "list"; /* and show table */
}

if ($fce == "" or $fce == "list")
{
   $cmd = "SELECT * FROM colors";
   $results = $connection->query ($cmd);
   ?>
   
   <table border="1">
   <tr>
   <th>Name</th>
   <th>Red</th>
   <th>Green</th>
   <th>Blue</th>
   <th>Preview</th>
   <th>Edit</th>
   </tr>
   <?php
      while ($data = $results->fetch ())
      {
         echo "<tr> "; 
           for ($k = 0; $k < 4; $k++)
           {
              $value = $data [$k];
              echo "<td> $value  </td>";
           }
         $code = $data['red'].','.$data['green'].','.$data['blue'];
         echo "<td style='background-color:rgb(".$code.")'>  </td> "; 
         echo "<td> <a href=?fce=edit&id=" . urlencode ($data[0]) . ">edit</a> </td> "; 
         echo "</tr> "; 
      }
   ?>
   </table>
   
   <?php
}

if ($fce == "edit")
{
   $id = param ("id");
   $cmd = "SELECT * FROM colors WHERE name = " . sql_str ($connection, $id) .  ";";
   echo $cmd;
   $results = $connection->query ($cmd);
   $data = $results->fetch ()
   ?>
   
   <h4> Edit one record </h4>
   <form>
   <table>
   <tr> <td> Name:  </td> <td> <input type="text" name="name"  value= <?= sql_str ($connection, $data['name'])  ?> > </td> </tr>
   <tr> <td> Red:   </td> <td> <input type="text" name="red"   value= <?= sql_str ($connection, $data['red'])   ?> > </td> </tr>
   <tr> <td> Green: </td> <td> <input type="text" name="green" value= <?= sql_str ($connection, $data['green']) ?> > </td> </tr>
   <tr> <td> Blue:  </td> <td> <input type="text" name="blue"  value= <?= sql_str ($connection, $data['blue'])  ?> > </td> </tr>
   </table>
   <br>
   <input type="submit" value="Store">
   <input type="hidden" name="fce" value="store" >
   <input type="hidden" name="id" value=<?= sql_str ($connection, $id) ?> >
   </form>
   
   <?php
}

if ($fce == "store")
{
   $id = param ("id");

   $name = param ("name");
   $red = param ("red");
   $green = param ("green");
   $blue = param ("blue");
   
   echo "Store " . $id . " --> " . $name . ", " . $red . ", " . $blue . ", " . $green;
   
   $cmd = 'UPDATE colors ' .
          ' SET name = '  . sql_str ($connection, $name) . "," .
          '     red = '   . sql_str ($connection, $red) . "," .
          '     green = ' . sql_str ($connection, $green) . "," .
          '     blue = '  . sql_str ($connection, $blue) . 
          ' WHERE name = ' . sql_str ($connection, $id);

   $results = $connection->query ($cmd);
   
   ?>
   <br>
   <a href="?fce=list"> continue </a>
   <?php
}

?>

</body>
</html>

<!--

Fedora 23 administrator ( su - )
................................

yum install httpd

systemctl enable httpd
systemctl start httpd

jine verze Linuxu :
   apachectl start
   /etc/init.d/httpd start
   service httpd start

upravit soubor /etc/httpd/conf.d/userdir.conf
   # UserDir disabled
   UserDir public_html
   (* nikoliv UserDir enabled *)
   (* pripadne <Directory "/home/*/public_html"> *)
   
systemctl restart httpd

less /var/log/httpd/error_log 


Fedora 23 uzivatel
..................

chmod o+rx $HOME
mkdir $HOME/public_html


Testovaci soubory 
.................

echo '<html><body>hello</body></html>' > ~/public_html/test.html
echo '<?php phpinfo (); ?>' > ~/public_html/test.php
firefox http://localhost/~uzivatel


KMLinux ~/public_html/.htaccess
...............................

Options +Indexes +ExecCGI
AddHandler cgi-script .cgi .pl .py


MySQL
.....

yum install mysql-server

systemctl enable mariadb
systemctl start mariadb

mysql -u root

   show databases;
   create database mydb;
   use mydb;
   show tables;
   create table colors (name varchar(80) not null primary key, red int, green int, blue int);
   insert into colors values ("blue", 0, 0, 255);
   select * from colors;
   describe colors;

   CREATE USER 'uzivatel'@'localhost' IDENTIFIED BY 'heslo';
   GRANT ALL ON mydb.* TO 'uzivatel'@'localhost';
   
   (* source_file file_name; *)   
   

PHP
...

yum install php php-pdo php-mysqlnd phpMyAdmin

systemctl restart httpd

-->