Deleting from mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    Deleting from mysql

    Hi,

    There is a 'delete' link in home.php page, when it is clicked a row in the database is deleted. How do I write the code for this. I am using this line of code
    [HTML]<a href='delete.ph p?id=1'>Delete</a>[/HTML] ,
    then when link is clicked, it is directed to delete.php page. At the last I have used this line of code to redirect back to home.php page

    [HTML]echo '<META HTTP-EQUIV="Refresh" Content="0; URL=home.php">' ;
    exit;[/HTML]

    But here the problem is when I click the link, the delete.php page is entered and a blank page appears and again return to home.php page. How can I code in such a way that, when the delete link is clicked it should not redirect to delete.php page but stay in the home.php page itself and perform the actions in delete.php page.

    please help me to solve this problem


    With regards
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You would have to create an AJAX request.

    http://www.tizag.com/ajaxTutorial/ajaxphp.php

    Comment

    • coolsti
      Contributor
      • Mar 2008
      • 310

      #3
      I have not done this myself so I do not know for sure, but I don't think you need a background query using Ajax, although it may be a more elegant solution.

      I think your problem is what you are echoing. Try to use the "header" function of PHP to do a redirect to the page you wish to show. This will give you an error if you already print out anything to the user's browser before you invoke header() but you can write your delete.php script so that this does not happen.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        OP Said :
        How can I code in such a way that, when the delete link is clicked it should not redirect to delete.php page but stay in the home.php page itself and perform the actions in delete.php page.
        He wants to stay ON home.php. Therefore, you use ajax to perform a query in the background without a page refresh.

        Comment

        • coolsti
          Contributor
          • Mar 2008
          • 310

          #5
          No need to bicker about this. The elegant solution is of course to stay on the home page and use a background query, but that is not what he was trying to do with his script and the echo statement to "refresh".

          Another way to do it is to do the tasks in the default.php script and then redirect to the script that presented the home page. I just mention that possibility and point out the header() function.

          There are some reasons to refresh the page and not just do a background query using, for example, Ajax. It could be that the results that are shown on the page need to be updated after the deletion, and in many cases this can be accomplished a lot easier by just rebuilding the page rather than moving elements around with dynamic html or javascript.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            And lets not forget, while AJAX is a very handy and good looking thing, having any feature depend on client-side code is a very bad idea. You have no guarantee that the client-side code is actually run by your client.

            I would recommend having a link that sends you to a delete.php page, which would delete the row, give a success message, and then send you back.

            Alternatively, you could always have JavaScript re-write your links...
            That is; have PHP output the links as if you were using the above scenario, but print a JavaScript function in the header that would re-write all the delete links, replacing the link with a JavaScript function that would send a AJAX request.

            That way, it will look good on browsers that support it without breaking it on browsers that don't.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by Atli
              And lets not forget, while AJAX is a very handy and good looking thing, having any feature depend on client-side code is a very bad idea. You have no guarantee that the client-side code is actually run by your client.
              While this is true, it's slightly over-used.

              Anyone browsing with javascript turned off, should not be browsing.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by markusn00b
                While this is true, it's slightly over-used.

                Anyone browsing with javascript turned off, should not be browsing.
                Even so, you should always assume your user is the dumbest person on the planet, using the least helpful browser available, even if that is never the case.

                In any case. If you have your web depend on JavaScript, at least try to detect if your visitors have their JavaScript disabled and inform them that the 1980's are over.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Originally posted by markusn00b
                  While this is true, it's slightly over-used.

                  Anyone browsing with javascript turned off, should not be browsing.
                  Several people that I know have NoScript installed to protect themselves against XSS (https://addons.mozilla.org/en-US/firefox/addon/722). One of these individuals develops for a well-known internet security company -- not an internet lightweight by any means.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Once again, I stand with my pants down, my pride dissolved.

                    Comment

                    • gubbachchi
                      New Member
                      • Jan 2008
                      • 59

                      #11
                      Thank you for the reply' s.

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Originally posted by markusn00b
                        Once again, I stand with my pants down, my pride dissolved.
                        I think they have a pill for that....

                        Comment

                        • Atli
                          Recognized Expert Expert
                          • Nov 2006
                          • 5062

                          #13
                          Originally posted by pbmods
                          Several people that I know have NoScript installed to protect themselves against XSS
                          I never leave localhost without it :)

                          Comment

                          Working...