Page 1 of 1

DELETE FUNCTION

Posted: Wed Oct 23, 2013 7:13 am
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 } ?>
 
?>


Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 11:21 am
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.

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 11:52 am
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;
}	

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 2:51 pm
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!

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 3:03 pm
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...

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 3:21 pm
by Temor
Can you show me how your table looks?

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 3:40 pm
by louiegiezer
just like a blog tutorial of jacek...

posts
post_id
post_title
post_body
categories
post_user
post_date

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 3:51 pm
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

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 4:03 pm
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>

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 7:20 pm
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.

Re: DELETE FUNCTION

Posted: Wed Oct 23, 2013 10:12 pm
by ScTech
Use $post['post_id'] since it contains the post's id from the returned array in your function.

Re: DELETE FUNCTION

Posted: Thu Oct 24, 2013 2:52 pm
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 } ?>
 
?>
 

Re: DELETE FUNCTION

Posted: Thu Oct 24, 2013 3:27 pm
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>

Re: DELETE FUNCTION

Posted: Thu Oct 24, 2013 4:47 pm
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;
}