Hi All, a bit new to PHP. I've been reading Kevin Yank's book PHP Novice to Ninja and got way through part of the book so far but have come to a little stumbling block. I have two files, the first is a index,php which acts as the controller file and the other is form.php which has the form to add data. What I am trying to do is add an inline error validation for missing fields within form.php when the user submits the form. Index.php:
<?php
include '../includes/dbconn.php';
# add joke link pressed
if (isset($_GET['add_joke']))
{
$textError = '';
// Build the list of authors for drop-down list
try
{
$result = $dbConnection->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
$error = 'Error fetching list of authors.' . '<br />' . $e -> getMessage();
include '../includes/error.php';
exit();
}
foreach ($result as $row)
{
$authors_in_db[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
include 'form.php';
exit();
}
# add joke to the database
if (isset($_GET['add_joke_to_db']))
{
# if author/joke is blank
if ($_POST['joke_text'] == '' || $_POST['author'] == '')
{
$textError = 'You must add text to this field.';
}
# continue with adding joke to the database
try
{
$sql = 'INSERT INTO joke SET
joke_text = :joke_text,
joke_date = CURDATE(),
author_id = :author_id';
$s = $dbConnection -> prepare($sql);
$s -> bindValue(':joke_text', $_POST['joke_text']);
$s -> bindValue(':author_id', $_POST['author']);
$s -> execute();
}
catch (PDOException $e)
{
$error = 'Error adding joke.' . '<br />' . $e -> getMessage();
include '../includes/error.php';
exit();
}
header('Location: .');
exit();
}
# delete joke from the database
if (isset($_GET['delete_joke']))
{
try
{
$sql = 'DELETE FROM joke WHERE id = :id';
$s = $dbConnection -> prepare($sql);
$s -> bindValue(':id', $_POST['id']);
$s -> execute();
}
catch (PDOException $e)
{
$error = 'Error deleting joke.' . '<br />' . $e -> getMessage();
include '../includes/error.php';
exit();
}
header ('Location: .');
exit();
}
# select all jokes from the database
try
{
$sql = 'SELECT joke.id, joke.joke_text, joke.joke_date, author.name, author.email
FROM joke INNER JOIN author
ON author_id = author.id';
$result = $dbConnection -> query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching jokes.' . '<br />' . $e -> getMessage();
include '../includes/error.php';
exit();
}
# add each data item within an array
foreach ($result as $row)
{
$jokes_in_db[] = array(
'joke.id' => $row['id'],
'joke.joke_text' => $row['joke_text'],
'joke.joke_date' => $row['joke_date'],
'author.name' => $row['name'],
'author.email' => $row['email']
);
}
include 'jokes.php';
?>
form.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add Joke</title>
<link rel="stylesheet" type="text/css" href="../includes/styles.css" />
</head>
<body>
<h1>Add Joke</h1>
<form action="?add_joke_to_db" method="post">
<div>
<label for="joke_text">Type your joke here:</label>
<textarea id="joke_text" name="joke_text" rows="3"></textarea>
<span class="error">* <?php echo $textError;?></span>
</div>
<div>
<label for="author">Author:</label>
<select name="author" id="author">
<option value="">Select one</option>
<?php foreach ($authors_in_db as $data): ?>
<option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>">
<?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
</option>
<?php endforeach; ?>
</select>
<span class="error">* <?php echo $textError;?></span>
</div>
<div>
<input type="submit" value="Add">
</div>
</form>
</body>
</html>
database:
-- phpMyAdmin SQL Dump
-- version 4.6.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Nov 30, 2016 at 01:45 PM
-- Server version: 5.6.31
-- PHP Version: 7.0.8
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `jokesdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `author`
--
CREATE TABLE `author` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `author`
--
INSERT INTO `author` (`id`, `name`, `email`) VALUES
(1, 'Author1', '
[email protected]'),
(2, 'Author2', '
[email protected]'),
(3, 'Author3', '
[email protected]'),
(6, 'Author4', '
[email protected]');
-- --------------------------------------------------------
--
-- Table structure for table `joke`
--
CREATE TABLE `joke` (
`id` int(11) NOT NULL,
`joke_text` text,
`joke_date` date NOT NULL,
`author_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `joke`
--
INSERT INTO `joke` (`id`, `joke_text`, `joke_date`, `author_id`) VALUES
(1, 'Why did the chicken cross the road? To get to the other side!', '2016-07-11', 1),
(2, 'Knock-knock! Who\'s there? Boo! "Boo" who? Don\'t cry; it\'s only a joke!', '2016-06-30', 1),
(3, 'Knock, knock. Who’s there? Canoe! Canoe who? Canoe come out and play with me today?', '2016-08-05', 1),
(4, 'Knock, knock. Who’s there? Lettuce. Lettuce who? Lettuce in, it’s cold out here.', '2016-08-05', 2);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `author`
--
ALTER TABLE `author`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `joke`
--
ALTER TABLE `joke`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `author`
--
ALTER TABLE `author`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
--
-- AUTO_INCREMENT for table `joke`
--
ALTER TABLE `joke`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=46;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Could anyone be of assistance?
Thanks