Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2004
    Posts
    5

    Question help with a php delete script

    I need some help with a php script to delete records in a mysql table
    What I want is a page with a list of all the records and a corresponding check box.
    The user then selects the various records he wants to delete and then click the delete button to submit the form to a processing page.

    To create the form I do a select query on the table and then loop through them using a for loop adding a check box for each record with a name of check$i and a value of $gid. The problem I’m having is that when I submit the form using the get function I do not know what values to use for the $HTTP_GET_VARS[check$i] function. Is there any way to tell PHP to go and look at the URL and get the value of the check$i variable there. Or am I being completly stupid about this and need to look at a different way of doing it.

  2. #2
    Join Date
    May 2004
    Location
    Colorado
    Posts
    948
    well to get a variable from the url you would use $_GET['varname'];
    say you had a url:
    http://www.someothersite.com/index.php?page=3

    the $_GET['page'] would return 3

    to select the records and display all the ones that exist, i would define a variable that counted the number of rows you have (therefore the number of records) then go through them....
    ie:
    PHP Code:
    $result mysql_query("SELECT * FROM tablename"$link);
    $numRows mysql_num_rows($result);

    for(
    $i 0$i $numRows$i++){
    echo 
    'Out put the stuff you want to include from your records here, i assume you know how to do this'
    echo '<a href="index.php?delete='.$recordId.'">Delete</a>'

    then you would have the index page do something if $_GET['delete'] is set

    ie.
    if(!isset($_GET['delete']){
    echo 'there is nothing to delete';
    }else{
    //query your delete function here and have it delete the record where the id == the $_GET['delete'] id

    hope that helps you some, if you need any more help just ask


    Absolute Power, its the only way to roll!

  3. #3
    Join Date
    Sep 2004
    Posts
    5
    Thanks WraithGuard

    But what if I want the user to select multiple records for deletion. What I was thinking was to create a list of all records with check boxes for every corresponding record allowing the user to select every record they want to delete the first time (similar to deleting email in hotmail)

    The problem I’m having is that if I use the same name for the checkbox I end up with a URL looking like http://www.someothersite.com/index.php?delete=1&delete=2&delete=3
    If I then use $_GET[‘delete’] it only returns the last value “3”.

    PHP Code:
    <form action="untitled.php" method="get">
    <?
    $result = mysql_query("SELECT * FROM tablename", $link);
    $numRows = mysql_num_rows($result);

    for($i = 0; $i < $numRows; $i++)
    {
        $id = mysql_result($result, $i, "id");
        $details = mysql_result($result, $i, "details");

        echo "<input name='delete' type='checkbox' value='$id'><br> $details";
    }
    ?>
    <input type="submit" name="Submit">
    </form>
    I thought that it might help if I then create a unique name for every checkbox by appending the $i value of the loop to the name. The URL then looks like this http://www.someothersite.com/index.php?delete0=1&delete1=2&delete2=3
    I also assign the value of mysql_num_rows to a hidden field so that I would know how many possible records their where to begin with and thus how many delete$i values to look for on the processing page. Although this appears to work it seems like a very inefficient way of doing things. For example if I had 100 records and the user ticks only two of them the form will only post the two values ticked but I would still need to run through all 100 possibilities on the processing page to find the two that was ticked.

    PHP Code:
    <form action="untitled.php" method="get">
    <?
    $result = mysql_query("SELECT * FROM tablename", $link);
    $numRows = mysql_num_rows($result);

    for($i = 0; $i < $numRows; $i++)
    {
        $id = mysql_result($result, $i, "id");
        $details = mysql_result($result, $i, "details");

        echo "<input name='delete$i' type='checkbox' value='$id'><br> $details";
    }
    echo "<input name='numrows' type='hidden' value='$numRows'>";
    ?>
    <input type="submit" name="Submit">
    </form>
    If you know of a better way to do this I would really appreciate it. I have looked for some tutorials but could not find any on the specific subject.

  4. #4
    Join Date
    May 2004
    Location
    Colorado
    Posts
    948
    hmm, thats a fair question, off the top of my head i have no clue, and even after thinking about it for a few minutes i have come up with no solution.. i will think about it tonite and see if i can get it to work.. in the meantime, good luck with it

    will try to get back to you soon


    Absolute Power, its the only way to roll!

  5. #5
    Join Date
    Jul 2003
    Location
    UK
    Posts
    245
    2 questions

    1) is it possible to break a string into individual words in PHP?
    2) is it possible to concatenate the values of multiple check boxes into one string? (javascript maybe?)

    If both can be acheive, you can use a string to store the selection of the user and using a space (" ") or some other symbol as saperator for breaking them into individual words. examples of such a string are

    user choose 1, 2, and 4 : "1 2 4"
    user choose 10, 14, 20 and 50 : "10 14 20 50"

    using a string, you only need to send one parameter and it grow or shrink depanding on what the user hass chosen. But you still have to figure out how to build the string and then break it though.

    Just my thought.
    Last edited by NeoMT; 11-12-2004 at 05:43 AM.

  6. #6
    Join Date
    May 2004
    Location
    Colorado
    Posts
    948
    hmm thats an interesting idea.. might work havent thought about it though

    yes it is possible to break strings apart.. you use the explode() function

    PHP Code:
    $string1 "Hello | goodbye | string | nothere | adios";
    $string1 =  explode("|"$string); 
    that would give you an array named string1, you could then echo items out from the array or use them in ur code..

    echo $string1[0];

    would give you "hello"

    hope that helps you


    Absolute Power, its the only way to roll!

Posting Permissions

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