SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query String Record String

    Hiya Everyone,

    I currently have the following code that outputs all information contained in a table to the screen, whilst displaying a check box beside each one.

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name;
      echo $row->Species;
      echo $row->Sex;
      echo "<input type='checkbox' name='' />";
    
    }
      echo "<input type='submit' value='Submit' />";
    ?>
    Upon a record being selected and the submit button being clicked, how do I use a Query String to delete a record using an ID, but then re-number the ID column to reflect the number of records stored in the table?

    How do I do all the processing on the same page?

  2. #2
    SitePoint Enthusiast jagat_21's Avatar
    Join Date
    Jun 2007
    Location
    Ahmedabad,India
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can try something like this :

    while($row = mysql_fetch_object($result))
    {
    echo $row->Name;
    echo $row->Species;
    echo $row->Sex;
    echo "<input type='checkbox' name='remove_birds[]' value='" . $row->bird_id . "' />";

    }

    Now once the submit button is pressed then u can check the values of remove_birds variable using HTTP_POST_VARS or HTTP_GET_VARS.

    Try it...

  3. #3
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jagat_21 View Post
    Hi,

    You can try something like this :

    while($row = mysql_fetch_object($result))
    {
    echo $row->Name;
    echo $row->Species;
    echo $row->Sex;
    echo "<input type='checkbox' name='remove_birds[]' value='" . $row->bird_id . "' />";

    }

    Now once the submit button is pressed then u can check the values of remove_birds variable using HTTP_POST_VARS or HTTP_GET_VARS.

    Try it...
    I'm new to PHP, how do I actually create the remove_birds function?
    How do I do all the processing on the same page?

    What do you mean by:

    "Now once the submit button is pressed then u can check the values of remove_birds variable using HTTP_POST_VARS or HTTP_GET_VARS."

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Further to jagat's form, then delete like this:
    PHP Code:
    $selectedids $_POST['remove_birds'];
    for(
    $i=0$i count($selectedids); $i++)
        
    mysql_query("delete from Birds where bird_id='" $selectedids[$i] . "'"); 
    I think you dont have to re-numbering since the bird_id has been used in the check box.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Salchester View Post
    ... but then re-number the ID column to reflect the number of records stored in the table?
    please don't do that

    the id is used to give each row a unique column

    do not attempt to re-assign ids after deletions

    obtaining the number of rows in the table should be accomplished like this:
    Code:
    select count(*) from birds
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query String Record String

    do not attempt to re-assign ids after deletions
    Why Not?

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the table relates to other some tables and the ids are already stored in those related tables and you change the ids with renumbering the ids, uumm the data rows in the other tables will be lost (not really but will not be used).

    And the duplicate entries may occur in such case.

    Am i right @r937???
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    why not renumber? because it has serious and severe problems if any other tables are related, as rajug suggested

    also, even if there are no other related tables, it is a lot of processing, for absolutely no benefit whatsoever
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    why not renumber? because it has serious and severe problems if any other tables are related, as rajug suggested

    also, even if there are no other related tables, it is a lot of processing, for absolutely no benefit whatsoever
    r937,

    Thats OK, the table is standalone, plus the re-numbering of the records stored in the table will be used later on during development.

    How Can It Still Be Done?

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Salchester View Post
    How Can It Still Be Done?
    you will have to get the answer from someone else

    that is not "best practice" and i cannot support what you're doing
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  11. #11
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    you will have to get the answer from someone else

    that is not "best practice" and i cannot support what you're doing
    OK, never mind about re-numbering then, as I want to keep the code standard.
    Upon clicking a button next to each record, how do I use a Query String to delete a record using an ID column?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";;
      echo $row->Sex . "<br>";;
      echo "<input type='submit' name='Submit' value='Delete'><br>";
    }
    ?>


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
  •