I am not best at PHP and this job I need to do is PHP MUST BE, so I am asking for little help as I am still getting into the PHP.
Here are my tables:
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL UNIQUE,
`body` text NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) );
CREATE TABLE `topics` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL UNIQUE );
CREATE TABLE `post_topic` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`post_id` int(11) DEFAULT NULL UNIQUE,
`topic_id` int(11) DEFAULT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
FOREIGN KEY (`topic_id`) REFERENCES `topics` (`id`) );
There is ON UPDATE NO ACTION... but I left it out to keep it clean.
Now I added Admin page where I can add new posts and script that let me insert title, slug and body of the post
Here is PHP:
$id = "0";
$user_id = 3;
$title = "";
$slug = "";
$body = "";
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$slug = $_POST['slug'];
$body = $_POST['body'];
mysqli_query($db, "INSERT INTO posts (title, slug, body) VALUES ('$title', '$slug','$body')");
}
And here is HTML form
<form method="post" action="posts.php">
<input type="text" name="title" placeholder="Title">
<input type="text" name="slug" placeholder="Slug">
<textarea name="body" rows="3" placeholder="Text"></textarea>
<button type="submit" name="submit">Submit</button>
</form>
Also there is config.php file with DB connection and this part of my code works fine.
Now how can I add category using selection box to the post? For example I have three categories Sport, Tech and News and on each post I want to be able to select a category that later can be found under categoryes.
I tried few thing but it just adds new ID to the post_topic or topics table.
Edited
This is how inserts should be for example:
INSERT INTO `posts` (`id`, `user_id`, `title`, `slug`, `body`) VALUES (1, 1, 'First post', 'first-post','First post text')
INSERT INTO `topics` (`id`, `name`, `slug`) VALUES (1, 'Tech', 'tech')
INSERT INTO `post_topic` (`id`, `post_id`, `topic_id`) VALUES (1, 1, 1)