Thursday, 15 August 2013

INSERT INTO query doesn't work

INSERT INTO query doesn't work

I write a little CMS for my friend, and I start write Panel Admin. This is
my admin.php file:
<?php
$logged = 0;
require_once("../inc/mysql.php");
@session_start();
@session_regenerate_id();
if(file_exists('../inc/config.php')) {
require_once('../inc/config.php');
} else {
echo "Config.php error! Check this!";
die();
}
$db = new mysql($dbhost,$dbname,$dbuser,$dbpasswd);
if(isset($_GET['logout'])) {
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
header("Location: index.php");
}
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'blog': $page = "blog"; break;
case 'pages': $page = "pages"; break;
case 'gallery': $page = "gallery"; break;
case 'settings': $page = "settings"; break;
default: $page = "blog"; break;
}
}
else $page = "blog";
if(isset($_SESSION['sps_admin'])) {
$temp = explode('|',$_SESSION['sps_admin']);
$username = $temp[0];
$key = $temp[1];
$sth = $db->prepare("SELECT * FROM admin WHERE
username='".$username."' AND uid='".$key."';");
$sth->execute();
$result = $sth->fetch();
if(isset($result['id'])) {
$logged = 1;
}
else {
header("Location: login.php");
}
}
else {
header("Location: login.php");
}
if($logged == 1) {
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SPSFoto | Panel Admina</title>
<link
href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300&amp;subset=latin,latin-ext'
rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/fonts.css">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="../js/html5shiv.js"></script>
<script src="js/admin.js"></script>
<script src="http://tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
tinyMCE.init({
mode : "textareas",
height: 500,
editor_deselector : "mceNoEditor"
});
</script>
</head>
<body>
<nav id="admin_menu">
<div id="profile">
<h2>Witaj!</h2>
<h6><?php echo $username; ?></h6>
<div id="profile_buttons">
<a href="?logout" title="Wyloguj"><span
class="icon-switch"></span></a>
</div>
</div>
<div id="list">
<ul>
<li><a href="admin.php?page=blog" <?php if($page == "blog")
echo 'class="menu_active"'; ?>><span
class="icon-pencil"></span><p>Blog</p><span
class="arrow">></span></a></li>
<li><a href="admin.php?page=pages" <?php if($page == "pages")
echo 'class="menu_active"'; ?>><span
class="icon-copy"></span><p>Strony</p><span
class="arrow">></span></a></li>
<li><a href="admin.php?page=gallery" <?php if($page ==
"gallery") echo 'class="menu_active"'; ?>><span
class="icon-image"></span><p>Galeria</p><span
class="arrow">></span></a></li>
<li><a href="admin.php?page=settings" <?php if($page ==
"settings") echo 'class="menu_active"'; ?>><span
class="icon-cog"></span><p>Ustawienia</p><span
class="arrow">></span></a></li>
</ul>
</div>
</nav>
<section id="admin_site">
<header id="admin_top">
<div id="logo">
<a href="#">
<?php
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'blog':
echo "BLOG";
break;
case 'pages':
echo "STRONY";
break;
case 'gallery':
echo "GALERIA";
break;
case 'settings':
echo "USTAWIENIA";
break;
default:
echo "BLOG";
break;
}
}
else echo "BLOG";
?>
</a>
</div>
</header>
<section id="admin_content">
<div id="admin_content_text">
<?php
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'blog':
include("posts.php");
break;
case 'pages':
include("pages.php");
break;
case 'gallery':
include("gallery.php");
break;
case 'settings':
include("settings.php");
break;
default:
include("posts.php");
break;
}
}
else include("posts.php");
?>
</div>
</section>
</section>
</body>
</html>
<?php } ?>
As You can see i include posts.php when $_GET['page'] == "blog". So this
is posts.php file:
<?php
if(isset($_GET['item'])) {
switch ($_GET['item']) {
case 'addpost':
?>
<form action="admin.php?page=blog" method="POST">
<div class="addpost_h"><p>Tytu³: </p><input type="text"
name="addpost_title"></div>
<textarea name="addpost_text"
class="tinymce_editor"></textarea>
<input type="submit" name="addpost_submit"
class="form_submit" value="Wyœlij">
</form>
<?php
break;
}
}
else if(isset($_POST['addpost_submit'])) {
$date = date("d-m-Y");
$text = $_POST['addpost_text'];
$sth = $db->prepare("INSERT INTO posts VALUES (NULL,
'".$_POST['addpost_title']."', '".$text."', '".$date."');");
$sth->execute();
echo "Nowy post zosta³ dodany! <a href=\"admin.php?page=blog\">Kliknij
aby powróciæ do poprzedniej strony</a>";
}
else {
echo '<a href="?item=addpost">Dodaj nowy wpis</a>';
}
?>
I know that code is awful, but this is fast project :D I tried add some
post, all looks ok, but data aren't in the database. I connect to MySQL by
PDO, and that's ok - no errors. DB created, table too. I copy query from
this script and i use it in phpmyadmin sql and it's works!
I dont' know why that doesn't works in this scirpt. Can You help me?
EDIT: new posts.php file
<?php
if(isset($_GET['item'])) {
switch ($_GET['item']) {
case 'addpost':
?>
<form action="admin.php?page=blog" method="POST">
<div class="addpost_h"><p>Tytu³: </p><input type="text"
name="addpost_title"></div>
<textarea name="addpost_text"
class="tinymce_editor"></textarea>
<input type="submit" name="addpost_submit"
class="form_submit" value="Wyœlij">
</form>
<?php
break;
}
}
else if(isset($_POST['addpost_submit'])) {
$date = date("d-m-Y");
$text = $_POST['addpost_text'];
$sth = $db->prepare("INSERT INTO posts ('id','title', 'tresc',
'data_dodania') VALUES (NULL, '".$_POST['addpost_title']."',
'".$text."', '".$date."');");
$sth->execute();
echo "INSERT INTO posts ('id','title', 'tresc', 'data_dodania') VALUES
(NULL, '".$_POST['addpost_title']."', '".$text."', '".$date."');";
echo "Nowy post zosta³ dodany! <a href=\"admin.php?page=blog\">Kliknij
aby powróciæ do poprzedniej strony</a>";
}
else {
echo '<a href="?item=addpost">Dodaj nowy wpis</a>';
}
?>
But this doesn't work too. echo query returns: INSERT INTO posts
('id','title', 'tresc', 'data_dodania') VALUES (NULL, '12', '
12
', '15-08-2013');

No comments:

Post a Comment