Jump to content

Logic Problem


rawky1976

Recommended Posts

Hello all

 

I'm having a little trouble with the code below. I think it's either that the braces are misplaced or the statements are in the wrong order.

 

In the form if I echo'$recno' then I get the number that was passed from the previous page, but when I change it to echo'$dn' it's just blank. Is the variable not in scope of the form?

 

Also when the form is submitted the message that the record has been deleted is displayed, although it hasn't actually been deleted?

 

Thanks for any help you can offer, Mark

 

<h2>Confirm Record Delete</h2>

<?php
if (isset($_GET['recno'])) {
$recno = (int) $_GET['recno'];			         
}
include 'library/config.php';
include 'library/opendb.php';
$query = "select * from document WHERE document_id = '$recno'"; 
$result = mysql_db_query($query); 
if ($result) {
$r = mysql_fetch_array($result);
$dn = $r["document_name"];
} 
if (isset($_POST['submitted'])) {
$query = "DELETE FROM document WHERE document_id=$recno";
if (mysql_num_rows($result) == 1) { 
$result = mysql_query($query);
}
echo 'The record was sucessfully removed';
include ('./library/cpfooter.html');
exit();
}

		  
?>

<form action="confirmdelete.php" method="post">
<fieldset><legend>Confirm Record Deletion</legend>
<p>Are you sure you wish to remove the record for the document: -<br /><br />
<?php echo "$dn"; ?><br /><br />
<input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>

Link to comment
Share on other sites

Try this hope it works...

 

<?php
include 'library/config.php';
include 'library/opendb.php';

// check if rec no is set or not...
if (isset($_GET['recno'])) 
{
$recno = (int) $_GET['recno'];			         
}

//query 
$query = "select * from document WHERE document_id = '$recno'"; 
$result = mysql_query($query) or die(mysql_error()); 

// if result found
if ($result) 
{
$r = mysql_fetch_array($result);
$dn = $r['document_name'];
} 


// from here you are trying to delete the record...

if (isset($_POST['submitted']))
{
$query_del = "DELETE FROM document WHERE document_id=$recno";
$result_del = mysql_query($query_del);
echo 'The record was sucessfully removed';
include ('./library/cpfooter.html');
exit();
}

		  
?>
<h2>Confirm Record Delete</h2>
<form action="confirmdelete.php" method="post">
<fieldset><legend>Confirm Record Deletion</legend>
<p>Are you sure you wish to remove the record for the document: -<br /><br />
<?php echo "$dn"; ?><br /><br />
<input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>

Link to comment
Share on other sites

Hello, thanks for the response.

 

I now get the filename displayed on the confirm delete page ($dn is being assigned and displayed).

 

The delete success message is displayed but the record still isn't actually deleted.

 

I'll double check the fieldnames etc.

 

Mark

Link to comment
Share on other sites

n~ link=topic=170348.msg752741#msg752741 date=1196694992]

 

if (isset($_POST['submitted']))

<input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" />
[/quote]
I think you could safely replace those two with
[code]
if(isset($_POST['submDel'])) {

<input type="submit" value="Yes Delete Record" name="submDel" />

That's what I always do anyway...[/code]

Link to comment
Share on other sites

Hello again! I've been looking into this and I think I've discovered the problem.

 

The recno value comes as a GET from the previous page. When the form is submitted it POSTS, so the recno doesn't get populated when it reloads.

 

I've therefore updated the script as below. I've used another hidden field in the form and tried to assign the recno value to it for the post. I've also added an elseif at the top to catch both GET and POST.

 

I'm not sure whether you can do it that way though? Is the syntax correct for my new hidden field?

 

<?php
include 'library/config.php';
include 'library/opendb.php';

// check if rec no is set or not...
if (isset($_GET['recno'])) 
{
//$recno = (int) $_GET['recno'];
$recno = $_GET['recno'];			         
} elseif (isset($_POST['recno'])){
  $recno = $_POST['recno'];
}

// from here you are trying to delete the record...

if (isset($_POST['submitted']))
{
$query = "DELETE FROM knowledgebase.document WHERE document_id='$recno'";
$result = mysql_query($query) or die(mysql_error());
echo 'The record was sucessfully removed';
include ('./library/cpfooter.html');
exit();
}

//query 
$query_sel = "select * from document WHERE document_id = '$recno'"; 
$result_sel = mysql_query($query_sel) or die(mysql_error()); 

// if result found
if ($result_sel) 
{
$r = mysql_fetch_array($result_sel);
$dn = $r['document_name'];
} 

		  
?>

<form action="confirmdelete.php" method="post">
<fieldset><legend>Confirm Record Deletion</legend>
<p>Are you sure you wish to remove the record for the document: -<br />
<?php echo "$dn"; ?><br /><br />
<input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="recno" value="$recno">
</fieldset>
</form>

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.