Not sure how to go about making it so that I can delete and edit a post..
So I am stumped on how to make it to delete and edit post. The key factor for getting the information is the id field. And I am able to get it and display it, but how can I get it to work between all my functions?
This is what I have so far.
<?php
include 'test.php';
function connection()
{
$connection = @mysql_connect('localhost','root','') or die('could not connect to mysql');
$select_db = @mysql_select_db('tutorials') or die('could not connect to database');
}
function delete_post($id)
{
connection();
$query = "DELETE FROM `posts` WHERE `id` = '$id'";
$result = mysql_query($query);
}
function find_posts()
{
connection();
$query = 'SELECT `posts`.`title`, `posts`.`body`, `posts`.`id`, `users`.`username` FROM `posts`, `users` WHERE `posts`.`user_id` = `users`.`id`';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$id = $row['id'];
echo '<b>' . $row['title'] . '</b>' .'<a href=/practice/model/posts.php?action=delete>[delete]</a> <a href=/practice/model/posts.php?action=edit>[edit]</a>' . '<br/>';
echo $row['body'] . '<br/>';
echo $row['username'] . '<p>';
}
}
function find_post($id)
{
connection();
$query = "SELECT `posts`.`title`, `posts`.`body`, `posts`.`id`, `users`.`username` FROM `posts`, `users` WHERE `posts`.`user_id` = `users`.`id` AND `posts`.`id` = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo '<b>' . $row['title'] . '</b>' . '<br/>';
echo $row['body'] . '<br/>';
echo $row['username'] . '<p>';
}
function create_post()
{
connection();
$title = $_POST['title'];
$body = $_POST['body'];
$query = "INSERT INTO `posts` SET `title` = '$title', `body` = '$body', `created_at` = NOW(), `user_id` = 53";
$result = mysql_query($query);
if(!$result)
{
echo mysql_error();
}
}
delete_post(7);
find_posts();
?>
<?php
$action = $_GET['action'];
switch ($action)
{
case 'create':
echo 'created' . '<br/>';
create_post();
break;
case 'delete':
echo 'deleted' . '<br/>';
delete_post($id);
break;
}
?>
<form action="/practice/model/posts.php?action=create" method="POST">
<input type="text" col="40" name="title" /><br/>
<textarea cols="30" rows="5" name="body"> </textarea><br/>
<input type="submit" name="submit" />
</form>
So yea, I am not sure how to get the 'id' parameters linked between everything that needs it. Sorry for the vague description, but its kind of hard to put it into words. If you need a more detailed description please let me know and I will try to. Pretty much want to be able to delete and edit my posts.
Thank you!