Results 1 to 9 of 9

 

Thread: PHP Search & Delete

  1. #1
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts


    I have left my PHP bible at home today, but desperately need to create a PHP form that has a single field, and when someone completes it and clicks on a button, it searches a database and where there is a record that matches the entry, it deletes it.

    Anyone have the code quickly to hand?

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    128
    Thanks
    0
    Thanked 1 Time in 1 Post
    Do you just need the SQL ?

    $sql = "DELETE FROM tblTable WHERE ID=" . $_POST["FormField] ;
    mysql_query($sql,$mylink);

    That assumes the matching field is numeric - if text then :

    $sql = "DELETE FROM tblTable WHERE Field='" . $_POST["FormField] . "'";
    mysql_query($sql,$mylink);

    putting single quotes round the value.
    haggul

  3. #3
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts
    Nah its the whole lot I need, the Form, field, how its passed to the SQL statement etc

    May have to go home to get my bible and bash it out

  4. #4
    Fraser's Avatar
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,013
    Thanks
    1
    Thanked 1 Time in 1 Post
    Chris - can you piece something together from this

    http://www.createafreewebsite.net/phpmysql/delete.html

    and this

    http://www.createafreewebsite.net/ph...e.html#example

    If I had any spare time today I could have put together a full example but that's the best I can offer at the moment

  5. #5
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    128
    Thanks
    0
    Thanked 1 Time in 1 Post
    How about a self calling page as such (you'll need to setup your conn string in the $mylink variable :

    <?php
    if($_POST["mode"]=="doit")
    {
    $sql = "DELETE FROM tblTable WHERE Field='" . $_POST["FormField] . "'";
    mysql_query($sql,$mylink);
    }
    ?>

    <form action="mypage.php" method="post">
    <input type="hidden" name="mode" value="doit">
    <input type="text" name="FormField" size="5" maxlength="5">
    <input type="submit" name="Submit" value="Submit">
    </form>
    haggul

  6. #6
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts
    This is what I have so far;
    Code:
    <FORM METHOD=POST ACTION="<? echo "$PHP_SELF"; ?>">
    <TABLE BORDER="0" CELLSPACING="3" CELLPADDING="3">
    <TR>
        <TD VALIGN="TOP"><font size=2>Your details:</font></TD>
        <TD VALIGN="TOP"><INPUT TYPE="text" NAME="leave"></TD>
    </tr>
    <TR>
        <TD VALIGN="TOP" COLSPAN="2"><INPUT
    TYPE="submit" NAME="actionbutton" VALUE="Leave"></TD>
    </TR>
    </TABLE>
    </FORM>
    
    <?php 
    $user="user_name";
    $password="password";
    $database="data_base";
    $host="localhost";
    
    $connection = mysql_connect($host,$user,$password) or die ("Error: connection");
    $db = mysql_select_db($database,$connection) or die ("Error: db");
    
    $querydelete = "DELETE FROM table WHERE field='$leave'";
    mysql_query($sql,$mylink);
    $resultdelete = mysql_query($querydelet);
    
    ?>

  7. #7
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    128
    Thanks
    0
    Thanked 1 Time in 1 Post
    That all looks OK except the php will execute regardless so add the if condition and hidden field.

    Also not sure if you need the last line.

    Change

    mysql_query($sql,$mylink);

    to

    mysql_query($querydelete,$connection);
    haggul

  8. #8
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts
    Got it working

    OK, so now it deletes the record, but whats the best way to get it to go to a different page now once its completed? Its been a while since I last scripted anything as you can tell lol

  9. #9
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    128
    Thanks
    0
    Thanked 1 Time in 1 Post
    Oh right - either just get the form to go to that page in it's action parameter and move the deletion code into that page - or just add :

    header("Location: diffpage.php");
    haggul



Thread Information

Users Browsing this Thread

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

Similar Threads

  1. MSN announced a redesign for its MSN Search service
    By getvisible in forum Affiliate Marketing Lounge
    Replies: 0
    Last Post: 25-03-04, 09:41 PM
  2. ARELIS 3 Link Popularity Software
    By Qui Gon Jinn in forum Media Coverage & PR Strategy
    Replies: 4
    Last Post: 05-03-03, 06:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2