www.webdeveloper.com
Results 1 to 11 of 11

Thread: deleting records using button

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    10

    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?

    Code:
          <?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")
    	 ?>" />
            </p>
            <?php } while ($row_myRdset = mysql_fetch_assoc($myRdset)); ?>
    </form>:confused:
    PLease help me in this question!!

  2. #2
    Join Date
    Apr 2008
    Posts
    190
    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
    Posts
    10
    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,
    Atomicc

  4. #4
    Join Date
    Apr 2008
    Posts
    190
    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:

    http://www.tizag.com/phpT/postget.php

  5. #5
    Join Date
    Mar 2010
    Posts
    10
    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)

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

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

    Code:
     <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.



    Code:
    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
    Posts
    2,477
    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.

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



Recent Articles