Muzes zkusit rozchodit tohle
<?php
@ini_set('error_reporting', E_ALL);
@ini_set("display_errors", "on");
error_reporting(E_ALL);
$CFG = array(
'debug' => true,
'host' => 'localhost',
'user' => 'root',
'psw' => '',
'db' => 'test'
);
function getRequest ($name, $default='')
{
return isset($_POST[$name]) ? $_POST[$name] : (isset($_GET[$name]) ? $_GET[$name] : $default);
}
function sql($query)
{
global $CFG;
global $conn;
$result = mysql_query($query,$conn);
if (!$result && $CFG['debug'])
{echo die('Chyba mysql_query. ' . mysql_error());}
return $result;
}
$conn = mysql_connect($CFG['host'],$CFG['user'],$CFG['psw']) or die ('Chyba mysql_connect. ' . mysql_error());
$db = mysql_select_db($CFG['db']) or die ('Chyba mysql_select_db. ' . mysql_error());
sql("SET NAMES utf8");
$data = array();
$where = array();
$do = getRequest('do');
switch ($do)
{
case 1: // insert uzivatele
$data['name'] = getRequest('name'); // - nacist data
foreach ($data as $key=>$value) // - upravit data pro query
{$data[$key] = mysql_real_escape_string($value);}
$query = "INSERT INTO `x_users` (`%s`) VALUES ('%s')";
$query = sprintf($query,implode("`,`",array_keys($data)),implode("','",$data));
echo "<br/>".$query;
$result = sql($query);
break;
case 2: // insert do mesice
$data['iduser'] = getRequest('iduser'); // - nacist data
$data['month'] = getRequest('month'); // - nacist data
foreach ($data as $key=>$value) // - upravit data pro query
{$data[$key] = mysql_real_escape_string($value);}
$query = "INSERT INTO `x_months` (`%s`) VALUES ('%s')";
$query = sprintf($query,implode("`,`",array_keys($data)),implode("','",$data));
echo "<br/>".$query;
$result = sql($query);
break;
case 3: // delete uzivatele z mesice
$where['iduser'] = getRequest('iduser'); // - nacist where
$where['month'] = getRequest('month'); // - nacist where
foreach ($where as $key=>$value) // - upravit where pro query
{$where[$key] = "`".$key."`='".mysql_real_escape_string($value)."'";}
$query = "DELETE FROM `x_months` WHERE %s";
$query = sprintf($query,implode(" AND ",$where));
echo "<br/>".$query;
$result = sql($query);
break;
case 4: // select uzivatelu
$query = "SELECT CONCAT(`name`,' (id=',`iduser`,')') AS `name` FROM `x_users`";
echo "<br/>".$query;
$result = sql($query);
$list = '';
if ($result)
{
$list = array();
while (($row = mysql_fetch_assoc($result))!==false)
{
$list[] = $row['name'];
}
}
$list = "\n<p>Seznam uzivatelu</p>\n<p>".implode("<br/>",$list)."</p><hr/>";
echo $list;
break;
case 5: // select uzivatelu z mesice
$where['month'] = getRequest('month'); // - nacist where
$month = $where['month'];
foreach ($where as $key=>$value) // - upravit where pro query
{$where[$key] = "`".$key."`='".mysql_real_escape_string($value)."'";}
$query = "SELECT CONCAT(`b`.`name`,' (id=',`b`.`iduser`,')') AS `name` FROM `x_months` `a` LEFT JOIN `x_users` `b` ON `b`.`iduser`=`a`.`iduser` WHERE %s";
$query = sprintf($query,implode(" AND ",$where));
echo "<br/>".$query;
$result = sql($query);
$list = '';
if ($result)
{
$list = array();
while (($row = mysql_fetch_assoc($result))!==false)
{
$list[] = $row['name'];
}
}
$list = "\n<p>Seznam zapsanych v mesici \"".$month."\"</p>\n<p>".implode("<br/>",$list)."</p><hr/>";
echo $list;
break;
case 6: // update uzivatele
$data['name'] = getRequest('name'); // - nacist data
foreach ($data as $key=>$value) // - upravit data pro query
{$data[$key] = "`".$key."`='".mysql_real_escape_string($value)."'";}
$where['iduser'] = getRequest('iduser'); // - nacist where
foreach ($where as $key=>$value) // - upravit where pro query
{$where[$key] = "`".$key."`='".mysql_real_escape_string($value)."'";}
$query = "UPDATE `x_users` SET %s WHERE %s";
$query = sprintf($query,implode("','",$data),implode("','",$where));
echo "<br/>".$query;
$result = sql($query);
break;
case 7: // select mesicu uzivatele
$where['iduser'] = getRequest('iduser'); // - nacist where
$iduser = $where['iduser'];
foreach ($where as $key=>$value) // - upravit where pro query
{$where[$key] = "`".$key."`='".mysql_real_escape_string($value)."'";}
$query = "SELECT `month` FROM `x_months` WHERE %s";
$query = sprintf($query,implode(" AND ",$where));
echo "<br/>".$query;
$result = sql($query);
$list = '';
if ($result)
{
$list = array();
while (($row = mysql_fetch_assoc($result))!==false)
{
$list[] = $row['month'];
}
}
$list = "\n<p>Seznam mesicu zapsanych uzivatelem s id = \"".$iduser."\"</p>\n<p>".implode("<br/>",$list)."</p><hr/>";
echo $list;
break;
case 8: // vypis celou tabulku
$name = getRequest('table'); // - nacist where
$query = "SELECT * FROM `".$name."`";
$result = sql($query);
if ($result)
{
$tbody = '';
$thead = '';
while (($row = mysql_fetch_assoc($result))!==false)
{
$tr = '';
foreach ($row as $key=>$value)
{
$tr .= sprintf("\n<td>%s</td>",$value);
}
$tbody .= sprintf("\n<tr>%s</tr>",$tr);
if ($thead=='')
{
$tr = '';
foreach ($row as $key=>$value)
{
$tr .= sprintf("\n<th>%s</th>",$key);
}
$thead .= sprintf("\n<tr>%s</tr>",$tr);
}
}
if ($tbody!=='')
{
echo sprintf("\n<table border=\"1\">\n<thead>%s</thead>\n<tbody>%s</tbody></table>",$thead,$tbody);
}
else {
echo "<p>Nula zaznamu</p>";
}
}
break;
default: break;
}
/*
--- mysql_priklad.sql ---
CREATE TABLE `x_users` (
`iduser` int(10) unsigned NOT NULL auto_increment,
`name` varchar(40) default NULL,
PRIMARY KEY (`iduser`),
INDEX `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE `x_months` (
`idmonth` int(10) unsigned NOT NULL auto_increment,
`month` int(10) unsigned default NULL,
`iduser` int(10) unsigned default NULL,
PRIMARY KEY (`idmonth`),
INDEX `iduser` (`iduser`),
INDEX `month` (`month`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
*/
?>
<p>Prikazy</p>
<ul>
<li><a href="?do=1&name=Lojza">Pridej noveho Lojzu do tabulky uzivatele</a> (INSERT)</li>
<li><a href="?do=6&iduser=4&name=Tomas">Zmen uzivateli s id = 4 jmeno na Tomas</a> (UPDATE)</li>
<li><a href="?do=2&iduser=1&month=5">Pridej uzivatele s id = 1 do tabulky mesice pod mesic = 5</a> (INSERT)</li>
<li><a href="?do=2&iduser=2&month=5">Pridej uzivatele s id = 2 do tabulky mesice pod mesic = 5</a> (INSERT)</li>
<li><a href="?do=2&iduser=3&month=5">Pridej uzivatele s id = 3 do tabulky mesice pod mesic = 5</a> (INSERT)</li>
<li><a href="?do=2&iduser=1&month=7">Pridej uzivatele s id = 1 do tabulky mesice pod mesic = 7</a> (INSERT)</li>
<li><a href="?do=2&iduser=2&month=7">Pridej uzivatele s id = 2 do tabulky mesice pod mesic = 7</a> (INSERT)</li>
<li><a href="?do=3&iduser=2&month=5">Smaz uzivatele s id = 2 z tabulky mesice pod mesice = 5</a> (DELETE)</li>
<li><a href="?do=5&month=5">Zobraz seznam lidi v mesici = 5</a> (SELECT)</li>
<li><a href="?do=5&month=7">Zobraz seznam lidi v mesici = 7</a> (SELECT)</li>
<li><a href="?do=7&iduser=2">Zobraz seznam zapsanych mesicu uzivatele s id = 2</a> (SELECT)</li>
<li><a href="?do=4&month=7">Zobraz seznam lidi</a> (SELECT)</li>
<li><a href="?do=8&table=x_users">Zobraz tabulku uzivatele</a> (SELECT)</li>
<li><a href="?do=8&table=x_months">Zobraz tabulku lide v mesici</a> (SELECT)</li>
</ul>