Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with code Please

    Hi im trying to delete from the database and I just dont know why it wont display the 'title' from the database to then go on to delete the actual post??

    The posts are displaying ok and it all runs through the motions but will not complete the delete action. Here is my code for the delete page which gets the id from the indexpage which all works ok.
    Code:
    <?php
    session_start();
    require_once('includes/db.inc.php');
    $stmt = $mysqli->prepare("SELECT postID, title FROM posts WHERE postID = ?");
    $stmt->bind_param('i',$_GET['postID']);
    $stmt->execute();
    $stmt->bind_result($postID, $title);
    $stmt->fetch();
    $stmt->close();
    ?>
    <?php include('includes/header.inc.php'); ?>
    <section id="createPost">
    	<form method="post" action="deletePost.php">
    		<fieldset>
    			<legend>Delete Post</legend>
    			<input name="postID" type="hidden" value="<?php echo $postID; ?>">
    
    			<p>Are you sure you want to delete <?php echo $title; ?></p>
    
    
    			<input type="submit" name="newPostBtn" value="Delete Post" id="newPostBtn"/>
    		</fieldset>
    	</form>
    </section><!--end createPost-->
    The p tag in the line are you sure you want to delete... does not return the $title of the post how can I check it is connected to the DB or at least reading from the database.

    Thanks
    This is problem 1...

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    After $stmt->execute() add printf("Selection returned %d rows", $stmt->num_rows);. Does that specify that 1 result has been returned?
    If so, I can't test this right now and it looks alright here, but you may try to remove the $stmt->close to after the output block. Best I know closing a statement should not affect the last results pulled into $postID, $title, but I could be mistaken.

  • Users who have thanked Fou-Lu for this post:

    trikkky (01-25-2012)

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It returns 0 rows..
    I havent changed my db.connect file i just include it and all the rest of the query runs a while statement from the index page which gets all the posts from the database.
    Any ideas what to try. the site is just a test for me to learn.
    http://concepthq.net63.net/
    Thank you

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Verify the value of $_GET by printing printf("GET['postid'] = '%s'" . PHP_EOL, $_GET['postID']);. If that does not show a valid postID, then the problem is you are not passing the GET. Ensure it is a number as well with no leading or trailing spaces (which you will be able to see with the ' ' around the number.

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    the answer is just an empty " so im not getting my _GET value from the index page here is my code:
    Code:
    if($result = $mysqli->query("SELECT * FROM posts ORDER BY postID desc"));
    		{
    			if ($result->num_rows > 0)
    				{
    												
    					while ($row = $result->fetch_object())
    						{
    						?>
    							<section id="primary"><article class="postItem">
    							<img src="images/<?php echo $row->image ?>" alt="thePostImage" />
    							<header><h2><?php echo $row->title ?></h2></header>
    							<p><?php echo $row->content ?></p>
    							<p class="author"><span>Author: </span> <?php echo $row->author ?></p>
    							<p class="date"><span>Created: </span> <?php echo $row->created ?></p>
    							<p><a href="edit.php?id=<?php echo $row->postID ?>">Edit</a><a href="delete.php?id=<?php echo $row->postID ?>"> Delete</a></p>
    							<p><a href="posts.php">Read More... </a></p>
    							</article></section>
    						<?php }
    				}
    				else
    				{
    					echo "NO results were found in the database!";
    				}
    		}
    
    		$mysqli->close();
    ?>
    Last edited by trikkky; 01-25-2012 at 06:13 PM. Reason: the delete.php?= the id from the $row->postID and that is what is in the browser when i am trying to delete

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That would be correct.
    What's the parsed HTML source result for this block that you have just posted above?

  • Users who have thanked Fou-Lu for this post:

    trikkky (01-25-2012)

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    on my delete?=... should I have the 'postID' that matches my database postID? or doesnt it matter when you view source it has the correct postID

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh, just noticed here. Your querystring is id=, not postID=. Change either the processing code to match the id, or change the querystring to use postID.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •