Jump to content

getting the id param to work so I can delete and edit posts.


Clandestinex337

Recommended Posts

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!

Link to comment
Share on other sites

Looking at your switch() statement I think you have the right idea, you just need to take it a step further. Based on that second code-block you are passing an "action" value on the URL to determine, well, the actino. But, you also need to pass the ID of the record you want to perform the action on - if the action needs it. A delete action would require the ID.

 

So, let's back up a second. Let's say you have a page that displays all the records. You would perform a DB query to get the information to disoplay the details for each record. Then, next to each record you might have a delete link. The link would be a URL containing two parameters: action=delete&id=[##], where [xx] is the id of the record. Then when you click the link you will use the action value in your switch stement to determine that the delete function should be called. When doing so you would use the $_GET['id'] value to pass to the delete function.

 

Does that make sense?

Link to comment
Share on other sites

Thank you for the reply, and yes it does make sense.. But for some reason I can't think of a way to link the id between all the pages. I know how to retrieve the id. In the find_posts function I retrieve the id parameter and echo it out, I just don't know how to link it to the delete function to make it work properly.

 

Thanks again. 

Link to comment
Share on other sites

Thank you for the reply, and yes it does make sense.. But for some reason I can't think of a way to link the id between all the pages. I know how to retrieve the id. In the find_posts function I retrieve the id parameter and echo it out, I just don't know how to link it to the delete function to make it work properly.

 

I thought I already explained that, but I'll try again.

 

You apparently have a link that looks something like this:

<a href="somepage.php?action=delete">Delete Record</a>

 

You need to add an additional parameter to the user to add the ID. I would assume you are using a DB query to display the records, so you should have the ID available to add to the link

<a href="somepage.php?action=delete&id=12">Delete Record</a>

 

Then on your page that processes the results you would do something like this:

switch ($_GET['action'])
{
    case 'create':
        echo 'created' . '<br/>';
        create_post();
        break;

    case 'delete':
        echo 'deleted' . '<br/>';
        delete_post((int) $_GET['id']);
        break;
}

Link to comment
Share on other sites

Ok, I was able to retrieve the id variable in the href. Now to get the switch to work, how I currently have it setup you have to go to posts.php?action=delete but my link to delete is going to posts.php?action=delete&id=$id so I am not to sure how to get it to ignore the &id=$id and just if its at action=delete it will run the switch.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.