TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login GOLD PRICE, RATE
login HOROSCOPE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login FINANCE, BUSINESS
login WORLD COUNTRIES, WIKI

PHP scripts, code, examples, tutorial

Previous articlePage bottomNext article  ALL TOPICS

PHP. MYSQL. CRUD example: Create Retrieve Update Delete

 

PHP. MYSQL. CRUD framework example: Create Retrieve Update Delete



eventConf.php 
<?php

global $_CONFIG, $_GET, $_POST, $_GP;

$_CONFIG = array (

"server"       => "localhost",
"user"         => "testas",
"password"     => "testas",
"db"           => "testas",

"wwwDomain"    => "127.0.0.1",

// Duomenu konfiguracija

"tbl1"         =>"testas",
"fields1"      => array ("id", "name", "type"),
"names1"      =>  array ("Nr.", "Vardas", "Tipas"),

);

?>

eventDesignAndForms.php:
<?php

function pageHead(){

$content = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" >
<title>Projektas</title>
<meta name=\"description\" content=\"Projektas apie mokslÄ…: programavimas, PHP\" />
<meta name=\"keywords\"    content=\"Projektas, mokslas, programavimas, PHP\"   />
<link href=\"./css/main.css\" rel=\"stylesheet\" type=\"text/css\" >
</head>
<body bgcolor = \"#FFEEAA\" >
<table width = 800 align = center><tr><td bgColor = \"#FFF1E1\" >
";

return $content;

} // pageHead

function pageEnd(){

$content = "</td></tr></table><body></html>";
return $content;

} // pageEnd


function formSearch($field){


global   $_CONFIG, $_GP;
extract ($_CONFIG);


//print_r ($fields);



$content = "\n\n<BR><FORM action = http://$wwwDomain method = GET >

Search <INPUT type = \"text\" name = \"$field\" value = \"{$_GP[$field]}\"><br>
<INPUT type = submit value = OK>
</FORM>\n\n";

return $content;

} // pageEnd


function formInsert($fields, $names){


global   $_CONFIG, $_GP;
extract ($_CONFIG);


print_r ($fields);


foreach ($fields as $k=>$v){
if ($v == 'id') continue ;
$contentFields .= "{$names[$k]}<INPUT type = \"text\" name = \"$v\" ><br>";

}

$content = "\n\n<BR><FORM action = http://$wwwDomain method = GET >

$contentFields

<INPUT type = hidden name = op   value =1>      
<INPUT type = hidden name = opCh value =1>
<INPUT type = submit value = OK>
</FORM>\n\n";

return $content;

} // pageEnd


function formUpdate($fields, $names, $row){


global   $_CONFIG, $_GP;
extract ($_CONFIG);


//print_r ($fields);


foreach ($fields as $k=>$v){
if ($v == 'id') continue ;
$contentFields .= "{$names[$k]}<INPUT type = \"text\" name = \"$v\" value = \"{$row[$v]}\"><br>";

}

$content = "\n\n<BR><BR><FORM action = http://$wwwDomain method = GET >

$contentFields

<INPUT type = hidden name = op   value =2>  \n    
<INPUT type = hidden name = opCh value =1>  \n
<INPUT type = hidden name = id value =\"{$row['id']}\"> \n
<INPUT type = submit value = OK>
</FORM>\n\n";

return $content;

} // pageEnd

?>

eventFunctionsData.php:
<?php

function connect(){

global   $_CONFIG;     
extract ($_CONFIG);


$link = mysql_connect($server, $user, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}


$db_selected = mysql_select_db($db, $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}


$sql = "CREATE TABLE IF NOT EXISTS `testas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`type` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM";  

mysql_query($sql);      

}

function gp(){

global   $_CONFIG, $_GET, $_POST, $_GP;
$_GP = array_merge ($_GET, $_POST);

}

function tableView($tbl, $fields, $names){

global   $_CONFIG, $_GP;
$content = "tableView<BR>";


$sql = "SELECT * FROM $tbl WHERE name LIKE '%{$_GP['name']}%'ORDER BY id";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {

$contentRow = "";
foreach($fields as $k => $v){

$contentRow .= "<TD>" .$row[$v] . "</TD>";

}

$edit   = "<A href = \"?op=2&id={$row['id']}\">Edit</A>";
$delete = "<A href = \"?op=3&id={$row['id']}\">Delete</A>"; 

$content .= "<TR>$contentRow<TD>$edit</TD><TD>$delete</TD></TR>";

}   


foreach($names as $k => $v){

$contentNames .= "<TD>$v</TD>";

}


$content = "<A href = \"?op=1\">Naujas</A><BR><TABLE border = 1 width = 60%><TR>$contentNames<TD> * </TD><TD> * </TD></TR>$content</TABLE>"; //. $content;
return $content; 

}

function tableNew($tbl, $fields, $names){

global   $_CONFIG, $_GP;
extract ($_CONFIG);

$content = "tableNew";

if (!$_GP['opCh'] ){

$content .=  formInsert($fields, $names);

}else{

foreach ($fields as $k=>$v){

if ($v == 'id') continue ;


if ($kk) $comma = " , ";
$kk++;

$contentFields .= "$comma $v = '{$_GP[$v]}' ";

}

$sql = "INSERT INTO $tbl SET $contentFields ";
echo $sql ; 
mysql_query($sql) or die(mysql_error());
$content .= "<BR>tableNew irasymas";
$content .= "<SCRIPT>window.location='http://$wwwDomain'</SCRIPT>";

}


return $content;      

}

function tableEdit($tbl, $fields, $names){

global   $_CONFIG, $_GP;
extract ($_CONFIG);
$content = "tableEdit";



if (!$_GP['opCh'] ){


$sql = "select * from $tbl where id = '{$_GP['id']}'";
echo $sql;
$result = mysql_query($sql) or die(mysql_error());

$row = mysql_fetch_assoc($result);

echo "<BR>Row: "; print_r ($row); echo "<BR>";

$content .=  formUpdate($fields, $names, $row);

}else{

foreach ($fields as $k=>$v){

if ($v == 'id') continue ;


if ($kk) $comma = " , ";
$kk++;

$contentFields .= "$comma $v = '{$_GP[$v]}' ";

}

$sql = "UPDATE  $tbl SET $contentFields WHERE id = '{$_GP['id']}'";
echo $sql ; 

mysql_query($sql) or die(mysql_error());
$content .= "<BR>tableEdit irasymas";
$content .= "<SCRIPT>window.location='http://$wwwDomain'</SCRIPT>";

}


return $content;      

}

function tableDelete($tbl){

global  $_CONFIG, $_GP;
extract ($_CONFIG);
$content = "tableDelete";

$sql = "DELETE FROM $tbl WHERE id = '{$_GP['id']}'";
//echo $sql; die();
$result = mysql_query($sql);
echo $wwwDomain; echo $sql;
$content = "<SCRIPT>window.location='http://$wwwDomain'</SCRIPT>";
echo $content;
return $content;      

}

?>

index.php:
<?php
session_start();

include_once "eventConf.php";
include_once "eventFunctionsData.php";
include_once "eventDesignAndForms.php";

connect (); gp();

extract ($_CONFIG); extract ($_GP); 
echo "<PRE>GP:"; print_r ($_GP); ; echo "Config:"; print_r ($_CONFIG); echo "</PRE>";

if (! $op ) {

$content = tableView($tbl1, $fields1, $names1);

}elseif($op==1){

$content = tableNew($tbl1, $fields1, $names1); 

}elseif($op==2){

$content = tableEdit($tbl1, $fields1, $names1); 

}elseif($op==3){

$content = tableDelete($tbl1); 

}

$content = pageHead() . formSearch("name") . $content . pageEnd();

print $content;

?>

 
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
The right man is the one that seizes the moment.
Johann Wolfgang Von Goethe
The realization that he is white in a black country, and respected for it, is the turning point in the expatriate's career. He can either forget it, or capitalize on it. Most choose the latter.
Paul Theroux
Through tattered clothes, small vices do appear. Robes and furred gowns hide all.
William Shakespeare