Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help with using forms to delete records from a database

    I am trying to make a PHP script that displays all of the records from a database, then allows a user to edit or delete the records via a form. I need a checkbox beside the names and a text field with a number beside each name. Is there a way to make it delete all names with a check beside them and update all number that have been changed when the user clicks submit? (this is what i currently have: http://www.cookevilleib.com/creditsedit.php)

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Hi frankmoss

    Do you have an 'id' for each record in your database. This would probably be best. You could then name each checkbox e.g. like this:

    PHP Code:
    echo '<input type="checkbox" name=delete['.$id.'] value="on">'
    This would give you an array on server side after form submission, e.g.

    PHP Code:
    $_POST['delete'][5] = 'on'
    $_POST['delete'][8] = 'on' 
    for the checkboxes that were checked. Then loop through the $_POST['delete'] array to find out which records to delete.

    Now you will need to make an extra submit button for deletion nest to the 'update' button. Give the submit buttons different name attribute to detect whether the user wants to delete or update.

    Code:
    <input type="submit" name="opDelete" value="Delete" />
    <input type="submit" name="opUpdate" value="Update" />
    Then do something like this:
    PHP Code:
    if($_POST['opDelete']) {
      
    // delete stuff
    }
    if(
    $_POST['opUpdate']) {
      
    // update stuff


  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

    I am very new to PHP and I still do not completely understand.

    PHP Code:
    $query "SELECT * FROM $usertable";
    $result mysql_query($query);
    if(
    $result) {
        echo 
    "<html>
    <head>
    <link href=beta.css rel=stylesheet type=text/css />
    </head>
    <body>
    <table width=750px align=center>
    <form method=post action=creditsedit.php name=creditedit>"
    ;
        while(
    $row mysql_fetch_array($result)){
            
    $name $row["Name"];
            
    $grade $row["Grade"];
            
    $credits $row["Credits"];
            echo 
    "<tr>
        <td class=left><input type=checkbox name=deletename value=$name> $name
        </td>
        <td class=left><input type=text name=gradeedit size=3 value=$grade>
        </td>
        <td class=left><input type=text name=creditedit size=5 value=$credits>
        </td>
        </tr>"
    ;
        }
        echo 
    "<tr><td>&nbsp;</td><td class=left><input type=submit name=opDelete value=Delete></td><td class=left><br><input type=submit name=opUpdate value=Update></tr></td></form></table></body></html>;

    I have this code that echos all of the records from a database table. I dont understand how it would delete multiple records.

    Right now my sql statement looks like this:
    PHP Code:
    if (isset($_POST['opDelete']))
    {
    $deletename $_POST['deletename'];
    mysql_query("Delete FROM $usertable WHERE 'Name' = $deletename");} 
    Is this right?

    And how do I use the [$id] you were talking about?

  • #4
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Try to use this loop for echoing your table. I changed the name attribute for the checkbox.
    PHP Code:
    while($row mysql_fetch_array($result)){
      
    $name $row["Name"];
      
    $grade $row["Grade"];
      
    $credits $row["Credits"]; 

      echo 
    "<tr><td class=left><input type=\"checkbox\" name=\"delete[$name]\" />$name</td>\n";
      echo 
    "<td class=left><input type=text name=gradeedit size=3 value=$grade></td>\n";
      echo 
    "<td class=left><input type=text name=creditedit size=5 value=$credits></td>\n";
      echo 
    "</tr>\n";

    To delete the multiple records that were checked, try like this
    PHP Code:
    if(isset($_POST['opDelete']) and isset($_POST['delete']))
    {
      foreach(
    $_POST['delete'] as $name => $dummy) {
        
    $sql "Delete FROM $usertable WHERE 'Name' = '$name'";
        
    mysql_query($sql);
      }

    What I meant with the $id was that it would probably be better to create a column in your table with an integer to identify each record. It is smoother if one references just the id. You would then just send the 'id's instead of the 'name's to delete.

    Tips. Put in some newlines "\n" like I did when you echo html. This makes it easier when you have to look at the source in your browser.

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What should the action on the form be?

  • #6
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    For now, let the form point to itself as it is now (so everything is collected in one file, would that be creditsedit.php ?) and put the snippet for deleting in the very top of your script right after the db connection part.
    PHP Code:
    <?php
    // file: creditsedit.php

    // db connection stuff
    if(isset($_POST['opDelete']) and isset($_POST['delete']))
    {
      ...
    }
    $query "SELECT * FROM $usertable";
    $result mysql_query($query);
    if(
    $result) {
        echo 
    "<html>
       <form method=\"post\" action=\"creditsedit.php\">"

    ...
    ?>

  • #7
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have to be doing something wrong...it still won't work.

    PHP Code:
    <?php
    $hostname
    ="***********";
    $username="*********";
    $password="**********";
    $dbname="Betaclubwebsite";
    $usertable="Credits";

    mysql_connect($hostname,$username$password) OR DIE ("Unable to connect to database! Please try again later.");
    mysql_select_db($dbname);

    if(isset(
    $_POST['opDelete']) and isset($_POST['delete'])) 

      foreach(
    $_POST['delete'] as $name => $dummy) { 
        
    $sql "DELETE FROM 'Credits' WHERE 'Name' = '$name'"
        
    mysql_query($sql); 
      } 
    }

    $query "SELECT * FROM $usertable";
    $result mysql_query($query);
    if(
    $result) {
        echo 
    "<html>
    <body>
    <table width=500 align=center>
    <tr>
    <td class=left><h3>Name</h3></td>
    <td class=left><h3>Grade</h3></td>
    <td class=left><h3>Credits</h3></td>
    </tr>
    <form method=\"post\" action=\"creditsedit.php\">"
    ;
       while(
    $row mysql_fetch_array($result)){
      
    $name $row["Name"]; 
      
    $grade $row["Grade"]; 
      
    $credits $row["Credits"];  

      echo 
    "<tr><td class=left><input type=\"checkbox\" name=\"delete[$name]\" />$name</td>\n"
      echo 
    "<td class=left><input type=text name=gradeedit size=3 value=$grade></td>\n"
      echo 
    "<td class=left><input type=text name=creditedit size=5 value=$credits></td>\n"
      echo 
    "</tr>\n"
    }
        echo 
    "<tr><td>&nbsp;</td><td class=left><br><input type=submit name=opDelete value=Delete>";
        echo 
    "</td><td class=left><br><input type=submit name=opUpdate value=Update></tr></td>";
        echo 
    "</form></table></body></html>";
    }
    ?>
    That's what I have. Do you see any problems? It's at http://www.cookevilleib.com/creditsedit.php if you want to see what it does.

  • #8
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Try removing your quotes around the table name (or use backticks `Credits`)
    PHP Code:
    $sql "DELETE FROM Credits WHERE 'Name' = '$name'"
    Generally it is a very good idea to put in die statements while testing at least Because php (deliberately) will not show database related errors
    PHP Code:
    mysql_query($sql) or die('Bad query: '.$sql); 

  • #9
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The back ticks did it! Thanks so much!

    Would I use a similar sql statement to update grade and credit numbers?

    And how do I make it ask the user if he is sure that he wants to delete the names?

  • #10
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    yeah, it would be quite similar. You would have to add another if-block next to the if-block for deleting--just with update queries in a loop.

    As for asking the user if he's sure he wants to delete you have to ask yourself if you want to rely on client-side scripting. You could then just add a few lines of javascript. You could also do it server-side with php usings sessions. For now, I would just choose the javascript solution.

    One more thing: The next thing you'll be asking. Now that your script is firing try to hit the refresh button after deleting or updating and you'll see a problem. Your requests contains post data. To avoid this problem, modify to this:
    PHP Code:
    if(isset($_POST['opDelete']) and isset($_POST['delete'])) 

      foreach(
    $_POST['delete'] as $name => $dummy) { 
        
    $sql "DELETE FROM Credits WHERE 'Name' = '$name'"
        
    mysql_query($sql); 
      } 
      
    header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
      exit();


  • #11
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The deleting part of the script works but I can't get the updating part to work.

    This is what i have:
    PHP Code:
    if(isset($_POST['opUpdate']) and isset($_POST['gradeedit'])) 

        
    $gradeedit $_POST['gradeedit'];
        
    $creditedit $_POST['creditedit'];
        foreach(
    $_POST['gradeedit'] as $namee => $dummy) { 
        
    $sql "UPDATE `Credits` SET `Grade` = '$gradeedit', `Credit` = '$creditedit' WHERE `Name` = '$name' LIMIT 1"
        
    mysql_query($sql) or die('Bad query: '.$sql);  
      } 
      
    header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); 
      exit(); 
    }

    ...

    <
    form method="post\" action=\"creditsedit.php\">";
       while(
    $row mysql_fetch_array($result)){
      
    $name $row["Name"]; 
      
    $grade $row["Grade"]; 
      
    $credits $row["Credit"]; 

      echo 
    "<tr><td class=left><input type=\"checkbox\" name=\"delete[$name]\" /> $name</td>\n"
      echo 
    "<input type=hidden name=name value=$name />";
      echo 
    "<td class=left><input type=\"text\" name=\"gradeedit[$name]\" size=\"3\" value=\"$grade\" /></td>\n"
      echo 
    "<td class=left><input type=\"text\" name=\"creditedit[name]\" size=\"5\" value=\"$credits\" /></td>\n"
      echo 
    "</tr>\n"
    }
        echo 
    "<tr><td class=left><br><input type=submit name=opDelete value=Delete />\n";
        echo 
    "</td><td>&nbsp;</td><td class=left><br><input type=submit name=opUpdate value=Update /></td></tr>\n"
    It gives me an error about an invalid "foreach"
    I also think that the variables arent right because it puts "Array" in the fields.
    Last edited by frankmoss; 01-26-2007 at 10:53 PM.

  • #12
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    You are a fast learner frankmoss...
    but you have been typing too fast

    echo "<td class=left><input type=\"text\" name=\"creditedit[$name]\" size=\"5\" value=\"$credits\" /></td>\n";

    Your update loop should also be slightly modified (notice you wrote $namee)
    PHP Code:
    if(isset($_POST['opUpdate']) and isset($_POST['gradeedit'])) 

      foreach(
    $_POST['gradeedit'] as $name => $dummy) {
          
    $gradeedit $_POST['gradeedit'][$name];
          
    $creditedit $_POST['creditedit'][$name];
          
    $sql "UPDATE `Credits` SET `Grade` = '$gradeedit', `Credit` = '$creditedit' WHERE `Name` = '$name'"
          
    mysql_query($sql) or die('Bad query: '.$sql);  
      } 
      
    header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); 
      exit(); 

    I'm affraid I gave you some misleading advice. These 2 lines: wait inserting them until your script is firing. Otherwise the redirect prevents you from seeing any warning messages that were displayed.
    PHP Code:
      header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); 
      exit(); 
    Still I recommend when you got things working, to add an `id` field in your table `Clients`

  • #13
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Haha...I fixed those spelling errors, but it keeps putting "array" in the database instead of numbers. Do you know why?

    Also I am planning on using an id when I get it working. Should I set it to auto_increment?

  • #14
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    I had slightly modified this part: which is why it inserts 'Array'

    $gradeedit = $_POST['gradeedit'][$name];
    $creditedit = $_POST['creditedit'][$name];

    Yes, just use auto_increment.

  • #15
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have this:
    PHP Code:
    if(isset($_POST['opUpdate']) and isset($_POST['gradeedit'])) 

    $gradeedit $_POST['gradeedit'][$name];
    $creditedit $_POST['creditedit'][$name];
        foreach(
    $_POST['creditedit'] as $name => $dummy) { 
        
    $sql "UPDATE `Credits` SET `Grade` = '$gradeedit', `Credit` = '$creditedit' WHERE `Name` = '$name' LIMIT 1"
        
    mysql_query($sql) or die('Bad query: '.$sql);  
      } 

    And it still inserts array...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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