Hi,
I am trying to set up a basic HTML form using PHP. It should create a text box and put the info in the database.
Here are the files which I am modifying from the book ‘PHP and MYSQL, Novice to Ninja’ by Kevin Yank, which is basically a tutorial on how-to build a CMS. (and a very good book!)
The table is called ‘topics’ and the field is ‘topic’. Perhaps there are too many variables called topics or topic. I am just getting returned to the index.php and hence the file search.html.php
index.php checks that the user is logged in and has roles, then it skips down to the end of this file and goes to searchform.html.php
Here is index.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/magicquotes.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/access.inc.php';
if (!userIsLoggedIn())
{
include '../login.html.php';
exit();
}
if (!userHasRole('Content Editor'))
{
$error = 'Only Content Editors may access this page.';
include '../accessdenied.html.php';
exit();
}
if (isset($_GET['add']))
{
$pageTitle = 'New Topics';
$action = 'addform';
$text = '';
$authorid = '';
$id = '';
$button = 'Add topics';
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
try
{
$result = $pdo->query('SELECT id, topic FROM topics');
}
catch (PDOException $e)
{
$error = 'Error fetching list of topics.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$topics[] = array('id' => $row['id'], 'topic' => $row['topic']);
}
include 'form.html.php';
exit();
}
if (isset($_GET['addform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
try
{
$sql = 'INSERT INTO topics SET
topic = :topic,
$s = $pdo->prepare($sql);
$s->bindValue(':topic', $_POST['text']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted topic.';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
include 'searchform.html.php';
searchform.html.php is just a link which creates a valueless variable ‘add’ in $_GET[‘add’]. This then goes back to index.php where the first if() statement is activated. This sets up variables for form.html.php. This form then submits to the second if() statement in index.php which sends the submitted data from the form to the database.
Here is searchform.html.php
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/artgibney/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Topics</title>
</head>
<body>
<h1>Manage Topics</h1>
<p><a href="?add">Add new topics</a></p>
<p><a href="..">Return to physCMS home</a></p>
<?php include '../logout.inc.html.php'; ?>
</body>
</html>
Here is form.html.phph
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/artgibney/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php htmlout($pageTitle); ?></title>
<style type="text/css">
textarea {
display: block;
width: 100%;
}
</style>
</head>
<body>
<h1><?php htmlout($pageTitle); ?></h1>
<form action="?<?php htmlout($action); ?>" method="post">
<div>
<label for="text">Type your topic here:</label>
<textarea id="text" name="text" rows="3" cols="40"><?php
htmlout($text); ?></textarea>
</div>
<input type="hidden" name="id" value="<?php
htmlout($id); ?>">
<input type="submit" value="<?php htmlout($button); ?>">
</div>
</form>
</body>
</html>
By problem is that after logging in, I click a link which send me to /artgibney/admin/topics/ which should then go to the index.php, which is above. But the page is blank. presumbly there is something wrong with the form being submitted.
Any help would be greatly appreciated.
Thanks,
Shane