Kit te chtel upozornit na to, ze to echo se vypise, at uz se podari film do db ulozit ci ne. Staci cokoliv poslat formularem. Klidne to muze byt falesny formular z jine stranky, treba z programujte.com.
Zkus si tam pridat pod ten formular jeste jeden
<form method=post><input type=hidden name=xxx value=yyy><input type=submit></form>
A klikni na nej, odeslat. Vypise ti to, ze pridal fulm, ale pritom v databazi zadny neni. A navic to muze zacit psat more chyb.
<?php
@ini_set('error_reporting', E_ALL);
@ini_set("display_errors", "on");
error_reporting(E_ALL);
//$dsn = "mysql:host=127.0.0.1;dbname=zoznam_filmov;charset=utf8";
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
$db = new PDO($dsn, "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$notice = array();
if (count($_POST)==4)
{
$query = "INSERT INTO filmy (nazov, rok, krajina, dlzka) VALUES (?, ?, ?, ?)";
$insert = $db->prepare($query);
$insert->execute(array($_POST['nazov'], $_POST['rok'], $_POST['krajina'], $_POST['dlzka']));
if ($insert)
{$notice[] = 'Film bol ˙speÜne pridany';}
}
$list = '';
$query = "SELECT nazov, rok, krajina, dlzka FROM filmy";
$select = $db->prepare($query);
$select->execute();
while(($row = $select->fetch(PDO::FETCH_ASSOC))!==false)
{
$cols = '';
foreach ($row as $key=>$value)
{
$cols .= '<td>'.$value.'</td>';
}
//$cols = '<pre>'.htmlspecialchars($cols).'</pre>';
$list .= '<tr>'.$cols.'</tr>';
}
$list = strlen($list)>0 ? '<table>'.$list.'</table>' : '';
$notice = count($notice)>0 ? '<p>'.implode('<br>',$notice).'</p>' : '';
/*
CREATE TABLE IF NOT EXISTS `filmy` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nazov` varchar(100) COLLATE utf8_czech_ci DEFAULT NULL,
`rok` varchar(100) COLLATE utf8_czech_ci DEFAULT NULL,
`krajina` varchar(100) COLLATE utf8_czech_ci DEFAULT NULL,
`dlzka` varchar(100) COLLATE utf8_czech_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `nazov` (`nazov`),
KEY `rok` (`rok`),
KEY `dlzka` (`dlzka`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
*/
?>
<!DOCTYPE html>
<html lang="cs-cz">
<head>
<meta charset="utf-8">
<meta name="description" content="Databßza filmov" />
<link rel="stylesheet" href="styl.css" type="text/css" />
<title>Filmy</title>
</head>
<body>
<header>
<div><h1>Databßza filmov</h1></div>
<nav>
<ul>
<li><a href="index.php">Zoznam filmov</a></li>
<li class="aktivni"><a href="pridat.php">PridaŁ film</a></li>
</ul>
</nav>
</header>
<?php echo $notice; ?>
<article>
<div id="centrovac">
<header>
<h1>PridaŁ film</h1>
</header>
<section>
<p>
<form method="post">
Nßzov:<br/>
<input type="text" name="nazov"/><br/>
Rok vydania:<br/>
<input type="text" name="rok"/><br/>
Krajina:<br/>
<input type="text" name="krajina"/><br/>
Dň×ka:<br/>
<input type="text" name="dlzka"/><br/>
<br/>
<input type="submit" value="PridaŁ film"/>
</form>
</p>
</section>
</div>
</article>
<p>Zoznam filmov</p>
<?php echo $list; ?>
</body>
</html>