Maracles Posted October 25, 2009 Share Posted October 25, 2009 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. Quote Link to comment Share on other sites More sharing options...
tivrfoa Posted October 25, 2009 Share Posted October 25, 2009 I would use AJAX. But the simple way, you can use header("Location: yourpage.php"); or you can use JavaScript: window.location.href = "yourpage.php"; Quote Link to comment Share on other sites More sharing options...
Maracles Posted October 25, 2009 Author Share Posted October 25, 2009 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. Quote Link to comment Share on other sites More sharing options...
Maracles Posted October 25, 2009 Author Share Posted October 25, 2009 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! Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 25, 2009 Share Posted October 25, 2009 Your productivity will increase dramatically with ajax http request, im able to process 1000's of database requests within a few minuets through my admin panel without 1 page reload. Quote Link to comment Share on other sites More sharing options...
Maracles Posted October 25, 2009 Author Share Posted October 25, 2009 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.