Results 1 to 11 of 11

Thread: deleting records using button

  1. #1
    Join Date
    Mar 2010

    deleting records using button

    My client wanna delete undesired messages from his miniblog.
    A repeating zone (code below) shows these posts and include a button to delete each of them. But it doesn't works out. Where is the error?

          <?php do { ?>
            <p class="style6">Em <?php echo $data = implode("/", array_reverse(explode("-", substr($row_myRdset['data'],0,10)))); ?>  &agrave;s <?php echo substr($row_myRdset['data'],11); ?>
            <?php echo $row_myRdset['nome']; ?>
            (mailto:<?php echo $row_myRdset['email']; ?>
            ) escreveu: <?php echo $row_myRdset['coment']; ?> e ID<?php echo $row_myRdset['id']; ?>: 
            <input name="submit" type="submit" id="id" value=<?php $id = $_GET["id"]?>;
    	 mysql_query("delete from guestbook where id = $id")
    	 ?>" />
            <?php } while ($row_myRdset = mysql_fetch_assoc($myRdset)); ?>
    PLease help me in this question!!

  2. #2
    Join Date
    Apr 2008
    You have some major flaws in your practices and understanding of how PHP works. PHP routines can not be run solely by a button click, they require a server side request of some kind.

    You have a few options to make the server side request.
    1) Have your submit button part of a form that does a POST or GET to another php page ('deletePost.php'). That page will contain your query to delete the post.
    2) Have your button run javascript that makes an asynchronous HTTP requests (AJAX) to another php page ('deletePost.php')

    You also have a syntax error with this code:

    PHP Code:
    <?php $id $_GET["id"]?>;
         mysql_query("delete from guestbook where id = $id")
    You closed your PHP prematurely, it should be:
    PHP Code:
    <?php $id $_GET["id"];
    mysql_query("delete from guestbook where id = $id")

    While this fixes the syntax error, it won't function like you want it(as I mentioned earlier).

    As for php practices, may I suggest altering your layout as readability suffers greatly. Personally I see no reason to jump in and out of php to print HTML when you are doing it so often and with such small blocks.

  3. #3
    Join Date
    Mar 2010
    you're right, kurby. I'm a newbie in PHP and I don't understand how it works out.
    Please, can you post the server-side delete function and its calling on client-side (the button code)?

    Thank you in advance.

    Sincerely yours,

  4. #4
    Join Date
    Apr 2008
    The your client would become MY client. I gave you some explanation, now its up to you to find out how to make it work.

    Heres a starting point:


  5. #5
    Join Date
    Mar 2010
    After studying some tutorials, I've reached to the following approach but it still is not working out. Let's see if you know where's the problem.

    File: main.php (client-side)

    <form name="form1" action="deletar.php" method="POST">
    It takes the variable to another page (the server-side file).

    <?php echo $row_myRdset['id']; ?>
    the valuable variable ID to be transmitted

     <input name="submit" type="submit" id="id" value="submit" />
    the button that submits the variable

    File: deletar.php (server-side)
    Built-in code from Dreamweaver from Server Behavior>Delete Record.

    if ((isset($_GET['id'])) && ($_GET['id'] != "")) {
      $deleteSQL = sprintf("DELETE FROM miniblog WHERE id=%s",
                           GetSQLValueString($_GET['id'], "int"));
      mysql_select_db($database_conn, $conn);
      $Result1 = mysql_query($deleteSQL, $conn) or die(mysql_error());
      $deleteGoTo = "main.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
        $deleteGoTo .= $_SERVER['QUERY_STRING'];
      header(sprintf("Location: %s", $deleteGoTo));
    A lot of guys use this dreamweaver's code but it's not working. Why?

  6. #6
    Join Date
    Nov 2008
    Quote Originally Posted by atomicc View Post
    A lot of guys use this dreamweaver's code but it's not working. Why?
    Probably because it was generated by a crappy piece of software rather than a sentient programmer. I don't know how people have the nerve to click some buttons in Dreamweaver without having a clue about what is actually going on, then charge clients for this stuff! If you don't know what you're doing, read some books and learn before taking people's money in return for some insecure spaghetti code spat out by your editor. Sorry if that sounds harsh but this practice is something of an annoyance to me.
    Last edited by Mindzai; 03-25-2010 at 02:08 PM.
    The first rule of Tautology Club is the first rule of Tautology Club.

  7. #7
    Join Date
    Apr 2008
    Your delete script is using $_GET and your form is POSTing.

  8. #8
    Join Date
    Mar 2010
    Mindzai -> First of all, I'm a newbie in php and just that would sufficy for you have some respect to me. Secondly, I earn for the whole project not for just a bit of code that is problematic. I can't spend too much time studying by now because I have deadlines to meet. If you're not interested to help your comments are not appreciated.

    Kurby, I've changed the form method to GET and nothing. Also changed all (server and client-side) to POST and nothing. Probably the error is when the variable id is passed to php but I can't see where is the bug.

    Answer if you know; nobody is obligated to reply.

  9. #9
    Join Date
    Apr 2008
    Your actually not passing a "id" value. You need to create an input element of some kind with name="id" and value="x". Then you can access $_GET['id'] which will be 'x'.

    HTML Code:
    <form action="deletar.php" method="post">
    <input type="hidden" name="id" value="4">
    <input type="submit" value="delete this">
    PHP Code:
    mysql_query('DELETE FROM table WHERE id='.$_POST['id']); 
    Last edited by kurby; 03-25-2010 at 04:14 PM.

  10. #10
    Join Date
    Mar 2010
    ok, it is working, the records are deleted. Only one problem: the script only deletes the first record, not what I chose. Each click deletes, id 5 , 6 ,7 and so on. Why?

    The delete button and "id" hidden field are on the repeating zone. It should work, right?

  11. #11
    Join Date
    Nov 2008
    Quote Originally Posted by atomicc View Post
    Mindzai -> First of all, I'm a newbie in php
    Yet you are happy to charge for your PHP skills...
    The first rule of Tautology Club is the first rule of Tautology Club.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11902 seconds
  • Memory Usage 2,961KB
  • Queries Executed 15 (?)
More Information
Template Usage (36):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (5)bbcode_code
  • (1)bbcode_html
  • (3)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (11)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (11)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates