...

View Full Version : Help with code Please



trikkky
01-25-2012, 05:33 PM
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.


<?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...

Fou-Lu
01-25-2012, 05:39 PM
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.

trikkky
01-25-2012, 05:46 PM
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

Fou-Lu
01-25-2012, 05:53 PM
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.

trikkky
01-25-2012, 06:10 PM
the answer is just an empty " so im not getting my _GET value from the index page here is my 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();
?>

Fou-Lu
01-25-2012, 06:16 PM
That would be correct.
What's the parsed HTML source result for this block that you have just posted above?

trikkky
01-25-2012, 06:22 PM
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

Fou-Lu
01-25-2012, 06:45 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum