Jump to content

[SOLVED] How to return to a previous page after running a PHP script


Maracles

Recommended Posts

I'm having a bit of a melt down here because I think this should be really easy but can't work it out...!

 

My page lists the records in my database and next to each record is a 'Delete' button. What I want to happen is once I click delete  my script should run the gives instructions to delete the record from the database after which it will refresh the page and show the database records again (obviously minus the one that has been deleted).

 

How can I do this?

 

Thanks in advance.

Link to comment
Share on other sites

Thank you for the reply. I have not yet started learning AJAX or Javascript yet (although Javascript seems to provide many solutions so I should better start learning soon!), however your PHP code worked to refresh the page. Unfortunately I am actually having trouble with the delete code; can you possible help with this? My code is below:

 

This is the table that lists the records and the button the controls delete:

 

<?php

for ($i=0; $i < $num_results; $i++) {
	  $row = mysql_fetch_assoc ($result);
      echo '<tr><td>';
      echo '<p><strong>'.($i+1).'.</td><td>';
      echo htmlspecialchars(stripslashes($row['title']));
      echo "</strong></td><td>";
      echo stripslashes($row['cxcode']);
      echo '</td><td>';
      echo stripslashes($row['reldate']);
      echo '</td><td>';
      echo stripslashes($row['strtdate']);
      echo '</td><td>';
      echo stripslashes($row['enddate']);
      echo '</td><td>';
      echo '<form action="stckprofile.php?id='.stripslashes($row['cxcode']).'" method="post"><input type="submit" value="View" name="stckview"></form>';
      echo '</td><td>';
      echo '<form action="delete_stck.php?id='.stripslashes($row['cxcode']).'"><input type="submit" value="Delete" name="stckdel"></form>';
      echo '</td>';
}

echo "</table>";

	mysql_free_result ($result);
	mysql_close ($db);

 

This is the code for the deletion script:

 

<?php

require_once('../Connections/sandbox.php'); 

$stockid = $_GET['id'];


$query = "DELETE FROM title WHERE cxcode = '$stockid'";

$result = mysql_query($query);
  if (!$result) {
  echo "mysql query not working";
}

header("Location: myportfolio.php");
?>

 

All that is happening at the moment is that the page is refreshing but the records are not being deleted.

 

Link to comment
Share on other sites

Sorry, I'm being an idiot and have noticed three errors in my own code!

 

1) I forgot to add the form method to the delete button

2) I forgot to add the '*' between DELETE and FROM!

3) My variables were not matching for the actual mysql_query!

 

Still not quite working but I can probably figure it out now. Sorry for wasting your time, it's getting late and I'm tired!

Link to comment
Share on other sites

It is definitely something I want to look into. I have only just started learning PHP and as you can see still trying to get to grips with basics!

 

I think the learning curve may have to be PHP, Javascript then Ajax.

 

Thanks for the help, finally got it working - most of the problems down to typos :-s!

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.