DELETE FUNCTION

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

DELETE FUNCTION

Post by louiegiezer »

http://betterphp.co.uk/board/viewtopic.php?f=4&t=849

i'm looking 4 delete function and i found this one... this code not work 4 me...
<?php
include 'core/init.php';
include 'includes/overall/header.php';
 
 
if (isset($_POST['action']) && $_POST['action'] === 'delete'){
    $errors = array();
 
    if (empty($errors)){
        delete_post($_GET['post_id']);
    }
}
 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
?>
 
<div class="table">
        <div class="tr-style">
                <div class="style1">
                        Title
                </div>
                <div class="style2">
                        Content
                </div>
                <div class="style3">
                        Categories
                </div>
                <div class="style3">
                        User
                </div>
                <div class="style4">
                        Date Published
                </div>
                <div class="style4">
                        Action
                </div>
        </div>
       
<?php
        foreach (get_all_post($page, 5) as $post)
                {
?>
 
<div class="clear"></div>
        <form action="" method="post"> 
                <div class="tr-style2">
                        <div class="style1a">
                                <?php echo $post['title'];  ?>
                        </div>
                        <div class="style2a">
                                <?php echo $post['body'];  ?>
                        </div>
                        <div class="style3a">
                                <?php echo $post['cat'];  ?>
                        </div>
                        <div class="style3a">
                                <?php echo $post['user'];  ?>
                        </div>
                        <div class="style4a">
                                <?php echo $post['date'];  ?>
                        </div>
                        <div class="style4a">
                                <a href="edit.php">Edit</a>
                        </div>                         
                        <div class="style4a">  
                                <a href="?action=delete" title="delete post">Delete</a>
                        </div> 
                </div>
        </form>
<?php } ?>
 
?>

ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: DELETE FUNCTION

Post by ScTech »

When using links, it uses the $_GET superglobal. Not $_POST. Also note you will need to include the post id in the link as well for what you're trying to do to work.
<?php while(!$succeed = try()); ?>
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: DELETE FUNCTION

Post by louiegiezer »

ill 4got to add this code... the delete function...

function delete_post($post_id){
    $post_id = (int)$post_id;
    mysql_query("DELETE FROM `posts` WHERE `post_id` = {$post_id}");
    mysql_query("DELETE FROM `comments` WHERE `post_id` = {$post_id}");
}

is this the nice way of fetching data?

function get_all_post($page, $per_page){
	$start 	  = (int)($page -1) * $per_page;
	$per_page = (int)$per_page;

	$sql = "SELECT
				`post_title` AS `title`,
			LEFT(`post_body` , 10) AS `body`,
				`categories` AS `cat`,
				`post_user` AS `user`,
				`post_date` AS `date`
			FROM `posts` 
			ORDER BY `post_id` DESC LIMIT {$start}, {$per_page}";
	
	$all_post = mysql_query($sql);
	
	$rows = array();
	while (($row = mysql_fetch_assoc($all_post)) !== false){
		$rows[] = array(
			'title'		=> $row['title'],
			'body'		=> $row['body'],
			'cat'		=> $row['cat'],
			'user'		=> $row['user'],
			'date'		=> $row['date']
			
			);
		}
	return $rows;
}	
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: DELETE FUNCTION

Post by Temor »

Okay, so it doesn't work. Does it give you an error or does it not do anything?

And is this thread about the delete function or about fetching data?.. Please keep things separate!
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: DELETE FUNCTION

Post by louiegiezer »

it doesnt error... its do nothing... this is 4 delete function only the fetching data is the one i want to delete it...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: DELETE FUNCTION

Post by Temor »

Can you show me how your table looks?
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: DELETE FUNCTION

Post by louiegiezer »

just like a blog tutorial of jacek...

posts
post_id
post_title
post_body
categories
post_user
post_date
ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: DELETE FUNCTION

Post by ScTech »

Did you change $_POST['action'] to $_GET['action'] and include the post id in the href like delete.php?action=delete&post_id=your_post_id
<?php while(!$succeed = try()); ?>
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: DELETE FUNCTION

Post by louiegiezer »

guy the delete function is working when i manually put the ?action=delete&post_id=16 at the end of my url... ill check the database and the post_id 16 is deleted.... i have some trouble at my delete button...

<a href="manage-post.php?action=delete&post_id=<?php echo $_GET['post_id']; ?>">Delete</a>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: DELETE FUNCTION

Post by Temor »

So if you click the link generated it does not bring you to a page with an ID in the URL?
Have you tried changing
<a href="manage-post.php?action=delete&post_id=<?php echo $_GET['post_id']; ?>">Delete</a>
to
<a href="manage-post.php?action=delete&post_id=<?php echo $_POST['post_id']; ?>">Delete</a>

I don't know what the code for that page looks like. Maybe if you post it we can be of more help.
ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: DELETE FUNCTION

Post by ScTech »

Use $post['post_id'] since it contains the post's id from the returned array in your function.
<?php while(!$succeed = try()); ?>
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: DELETE FUNCTION

Post by louiegiezer »

Temor wrote: I don't know what the code for that page looks like. Maybe if you post it we can be of more help.
<?php
include 'core/init.php';
include 'includes/overall/header.php';
 
 
if (isset($_POST['action']) && $_POST['action'] === 'delete'){
    $errors = array();
 
    if (empty($errors)){
        delete_post($_GET['post_id']);
    }
}
 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
?>
 
<div class="table">
        <div class="tr-style">
                <div class="style1">
                        Title
                </div>
                <div class="style2">
                        Content
                </div>
                <div class="style3">
                        Categories
                </div>
                <div class="style3">
                        User
                </div>
                <div class="style4">
                        Date Published
                </div>
                <div class="style4">
                        Action
                </div>
        </div>
       
<?php
        foreach (get_all_post($page, 5) as $post)
                {
?>
 
<div class="clear"></div>
        <form action="" method="post">
                <div class="tr-style2">
                        <div class="style1a">
                                <?php echo $post['title'];  ?>
                        </div>
                        <div class="style2a">
                                <?php echo $post['body'];  ?>
                        </div>
                        <div class="style3a">
                                <?php echo $post['cat'];  ?>
                        </div>
                        <div class="style3a">
                                <?php echo $post['user'];  ?>
                        </div>
                        <div class="style4a">
                                <?php echo $post['date'];  ?>
                        </div>
                        <div class="style4a">
                                <a href="edit.php">Edit</a>
                        </div>                        
                        <div class="style4a">  
                                <a href="?action=delete" title="delete post">Delete</a>
                        </div>
                </div>
        </form>
<?php } ?>
 
?>
 
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: DELETE FUNCTION

Post by Temor »

So it's about line 66.
 <a href="?action=delete" title="delete post">Delete</a>
you need to put the rest of the info in the url.
    <a href="manage-post.php?action=delete&post_id=<?= $post['id'] ?>" title="delete post">Delete</a>
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: DELETE FUNCTION

Post by louiegiezer »

what the heck.... after 3 days... i solved the problem.. i forgot to add id at my fetching data...
function get_all_post($page, $per_page){
	$start 	  = (int)($page -1) * $per_page;
	$per_page = (int)$per_page;

	$sql = "SELECT
				`post_id` AS `id`,
				`post_title` AS `title`,
			LEFT(`post_body` , 10) AS `body`,
				`categories` AS `cat`,
				`post_user` AS `user`,
				`post_date` AS `date`
			FROM `posts` 
			ORDER BY `post_id` DESC LIMIT {$start}, {$per_page}";
	
	$all_post = mysql_query($sql);
	
	$rows = array();
	while (($row = mysql_fetch_assoc($all_post)) !== false){
		$rows[] = array(
			'id'		=> $row['id'],
			'title'		=> $row['title'],
			'body'		=> $row['body'],
			'cat'		=> $row['cat'],
			'user'		=> $row['user'],
			'date'		=> $row['date']
			
			);
		}
	return $rows;
}	
Post Reply