+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Deleting from data base using value from form.

  1. #1
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Deleting from data base using value from form.

    It all works, but I have to put the "ID" value in the php code. How do I drag in a value from a text field?

    PHP Code:
    <html>
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>Administator Tools</title>
    </
    head>
    <
    body>
    <
    form name="dr" action="scripts/delete-r.php" id="dr">
    Delete by ID:<br />
    <
    input type="text" name="ID" size="4">
    <
    br />
    <
    br />
    Delete by Name:<br />
    <
    input type="text" name="NAME" size="30">
    <
    br />
    <
    br />
    Delete by Email:<br />
    <
    input type="text" name="EMAIL" size="30">
    <
    br />
    <
    br />
    Delete by IP(WIP):<br />
    <
    input type="text" name="IP" size="20">
    <
    br />
    <
    input type="submit">
    </
    form>
    </
    body>
    </
    html
    delete.php:
    PHP Code:
    <? 
    mysql_connect
    ('localhost''martynba_martynb''password') or die('Error: ' mysql_error());

    mysql_select_db('martynba_comments') or die('Error (DB): ' mysql_error());

    $del mysql_query("DELETE FROM commentTable WHERE ID='22'");
    if (!
    $del)
        {
        die(
    'Error: ' .mysql_error());
        }
    ?>
    Better yet, is there a way I can add a link to click on each row which will delete that row when displaying the data with this?:
    PHP Code:
    <?php
    // Connect to the database server
    mysql_connect('localhost''martynba_martynb''password') or die('Error connecting to the database, MySQL returned: ' mysql_error());
    // Select the database
    mysql_select_db('martynba_comments') or die('Error selecting database, MySQL returned: ' mysql_error());

    //Build query
    $query mysql_query("SELECT * FROM commentTable");

    //Display results
        
    while ($row mysql_fetch_array($query)) {
        echo 
    "<br /> ID: " .$row['ID']. 
        
    "<br /> First Name: ".$row['FNAME'].
        
    "<br /> Last Name: ".$row['LNAME'].
        
    "<br /> Phone Number: ".$row['PHON'];}
    ?>
    Last edited by martynball; 11-26-2009 at 02:51 AM.

  2. #2
    Mr. DOS is offline x10 Sophmore Mr. DOS is an unknown quantity at this point
    Join Date
    Oct 2009
    Location
    Nova Scotia, Canada
    Posts
    228

    Re: Deleting from data base using value from form.

    Same as you're doing here: use the $_POST global array. Or, you could use $_GET and call the delete like this: delete.php?id=42. That way, you could just link to the delete script from the list of items.

    Here's a good article on POST/GET in PHP.

    As a rule of thumb, though, anything that changes data should be set up as a POST, and anything that just retrieves data should be a GET. That way, someone can't inadvertently change data by repeatedly making the GET request.

    --- Mr. DOS
    Last edited by Mr. DOS; 11-25-2009 at 02:43 PM.
    I've written a couple articles on automatic application of AlphaImageLoader in IE6 using nothing but IE6-specific CSS rules.

  3. #3
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Re: Deleting from data base using value from form.

    I don't understand how i would insert the value into the WHERE thingy... is it something elike this:

    PHP Code:
    <? 
    mysql_connect
    ('localhost''martynba_martynb''password') or die('Error: ' mysql_error());

    mysql_select_db('martynba_comments') or die('Error (DB): ' mysql_error());

    $ID $_GET[ID]

    $del mysql_query("DELETE FROM commentTable WHERE ID='.$ID'");
    if (!
    $del)
        {
        die(
    'Error: ' .mysql_error());
        }
    ?>
    Last edited by martynball; 11-26-2009 at 02:51 AM.

  4. #4
    drf1229 is offline x10Hosting Member drf1229 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    71

    Re: Deleting from data base using value from form.

    Just some advice: NEVER give out your SQL password. I am warning you edit your password out of the code ASAP. Anybody can access your databases with your password and username.
    Edit:
    Although SQL is good to learn, I usually store my comments in a text file and read and write off it for a comments page. I find this way much simpler, though SQL wouldn't hurt to know.
    Edit:
    Quote Originally Posted by martynball View Post
    I don't understand how i would insert the value into the WHERE thingy... is it something elike this:

    PHP Code:
    <? 
    mysql_connect
    ('localhost''martynba_martynb''mlb0891sr3dm') or die('Error: ' mysql_error());

    mysql_select_db('martynba_comments') or die('Error (DB): ' mysql_error());

    $ID $_GET[ID]

    $del mysql_query("DELETE FROM commentTable WHERE ID='.$ID'");
    if (!
    $del)
        {
        die(
    'Error: ' .mysql_error());
        }
    ?>
    PHP Code:
    $del mysql_query("DELETE FROM commentTable WHERE ID='".$ID."'"); 
    Last edited by drf1229; 11-25-2009 at 09:10 PM. Reason: Automerged Doublepost

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Deleting from data base using value from form.

    Quote Originally Posted by Mr. DOS View Post
    As a rule of thumb, though, anything that changes data should be set up as a POST, and anything that just retrieves data should be a GET. That way, someone can't inadvertently change data by repeatedly making the GET request.
    Very good advice, this. It's part of the REST architectural style. Google turns up plenty of more technical descriptions, should you wish to go deeper.

    Quote Originally Posted by martynball View Post
    PHP Code:
    $del mysql_query("DELETE FROM commentTable WHERE ID='.$ID'"); 
    Almost. You're mixing both string concatenation (via the '.' operator) and string interpolation. You only need one of them:
    PHP Code:
    $ID mysql_real_escape_string($_POST['ID']);
    $statement "DELETE FROM commentTable WHERE ID='$ID'";
    // OR 
    $statement "DELETE FROM commentTable WHERE ID='" $ID "'"
    Note that I also sanitized the input with mysql_real_escape_string to prevent SQL injection (see also "SQL Injection Attacks by Example", "SQL Injection Walkthrough" and "Exploits of a Mom"). You could also cast $_POST['ID'] to an int, but this might inadvertently delete the row with ID 0.
    PHP Code:
    // Suppose $_POST['ID'] = "' or 1=1; --"
    $ID = (int) $_POST['ID'
    /* $ID is now 0, which prevents the injected code form deleting every comment, 
        but still might delete a comment we don't want to delete.
     */ 
    Nowadays, the preferred way of preventing SQL injection is to use prepared statements, which are well supported by the PDO driver:
    PHP Code:
    // manage DB connection in some other file named (e.g.) "localDB.php" with something like:
    function localDBConnection() {
        static 
    $db = new PDO('mysql:host=localhost;dbname=...''username''password'); 
        
    $db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        return 
    $db;
    }

    // in the script
    include("localDB.php");
    try {
        
    $db localDBConnection();
        
    $query $db->prepare('DELETE FROM commentTable WHERE ID=?');
        
    $query->execute(array($_POST['ID']));
        if (
    $query->rowCount() < 1) {
            
    // no rows deleted.
            
    echo "No matching comments, so nothing was deleted.";
        }
    } catch (
    PDOException $exc) {
        echo 
    "Internal database error. It's been logged; we'll look into it.";
        
    error_log($exc);

    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Re: Deleting from data base using value from form.

    It isnt working :/

    And cheers, I have edited my posts, I forgot to remove the password, please could you edit your quote.

    And another thing, how can I make this:
    //Display results
    echo "<table class=\"comment-table\">";
    echo "<tr>";
    echo "<td class=\"comment-name\">Posted by: $name<div class=\"comments-date\">$today</div></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td class=\"comment-message\">$message";
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td class=\"comment-id\">ID: $id";
    echo "<form method=\"post\" action=\"delete.php\" class=\"del-form\"><input name=\"userdel\" type=\"hidden\" value=\"$id\"><input type=\"image\" src=\"../images/del.png\"></form></td>";
    echo "</tr>";
    echo "</table>";
    echo "<br />";
    }

    Delete the current record?
    Last edited by martynball; 11-26-2009 at 03:18 AM.

  7. #7
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Deleting from data base using value from form.

    What are the errors you are getting?
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  8. #8
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Re: Deleting from data base using value from form.

    I'm not getting any, thats the weird thing. I think it is just not finding the correct values ect..

    http://martynleeball.x10hosting.com/testing/db-m.php

    Instead of doing that, could I just use this:
    //Display results
    echo "<table class=\"comment-table\">";
    echo "<tr>";
    echo "<td class=\"comment-name\">Posted by: $name<div class=\"comments-date\">$today</div></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td class=\"comment-message\">$message";
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td class=\"comment-id\">ID: $id";
    echo "<form method=\"post\" action=\"delete.php\" class=\"del-form\"><input name=\"userdel\" type=\"hidden\" value=\"$id\"><input type=\"image\" src=\"../images/del.png\"></form></td>";
    echo "</tr>";
    echo "</table>";
    echo "<br />";
    }

    so I an click the form and delete the record?
    Last edited by martynball; 11-26-2009 at 03:38 AM.

  9. #9
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Deleting from data base using value from form.

    In the form any one is required or all the fields are compulsory. Because i given a ID value and clicking submit giving parse error

    Parse error: syntax error, unexpected '=' in /home/martynba/public_html/testing/delete.php on line 2
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  10. #10
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Re: Deleting from data base using value from form.

    That error should be gone now, I was just messing around with the code.

+ Reply to Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. (immediate) !!!!my Data Base Any Help??
    By zahd13 in forum Free Hosting
    Replies: 4
    Last Post: 09-13-2009, 01:00 PM
  2. Transfer form data
    By driveflexfuel in forum Programming Help
    Replies: 2
    Last Post: 11-14-2008, 06:15 PM
  3. HTML help...
    By anuj_web in forum Programming Help
    Replies: 5
    Last Post: 05-08-2008, 11:22 AM
  4. Replies: 2
    Last Post: 11-20-2007, 11:15 PM
  5. my sql data base
    By mustaq in forum Free Hosting
    Replies: 6
    Last Post: 02-01-2006, 11:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers