www.webdeveloper.com
+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2003
    Location
    Australia
    Posts
    368

    Changing Data In MySQL Through PHP

    Hi,

    I have the following php page, it displays my members, by there Status... Approved, Deleted, On Hold etc... I want to add a 'Delete' next to each member in the table that gets displayed, but only want this 'Delete' button to move the members to the 'Delete' column. Which from a database POV would mean changing the member in the 'MemApp' column from an 'A' to a 'D'... Any help would be greatly appreciated!

    PHP Code:
    /* set the allowed order by columns */
    $default_sort 'LastName';
    $allowed_order = array ('JoinDate''FirstName','LastName''loginDateTime');

    /* if order is not set, or it is not in the allowed
     * list, then set it to a default value. Otherwise, 
     * set it to what was passed in. */
    if (!isset ($_GET['order']) || 
        !
    in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* construct and run our query */
    $query "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";

    $result mysql_query ($query);

    /* make sure data was retrieved */
    $numrows mysql_num_rows($result);
    if (
    $numrows == 0) {
        echo 
    "No data to display!";
        exit;
    }

    /* now grab the first row and start the table */
    $row mysql_fetch_assoc ($result);
    echo 
    "<TABLE border=1>\n";
    echo 
    "<TR>\n";
    foreach (
    $row as $heading=>$column) {
        
    /* check if the heading is in our allowed_order
         * array. If it is, hyperlink it so that we can
         * order by this column */
        
    echo "<TD><b>";
        if (
    in_array ($heading$allowed_order)) {
            echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\">$heading</a>";
        } else {
            echo 
    $heading;
        }                
        echo 
    "</b></TD>\n";
    }
    echo 
    "</TR>\n";

    /* reset the $result set back to the first row and 
     * display the data */
    mysql_data_seek ($result0);
    while (
    $row mysql_fetch_assoc ($result)) {
        echo 
    "<TR>\n";
        foreach (
    $row as $column) {
            echo 
    "<TD>$column</TD>\n";
        }
        echo 
    "</TR>\n";
    }
    echo 
    "</TABLE>\n";
    ?> 

  2. #2
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    PHP Code:
    <?php
    if (isset($_GET['del']) AND $_GET['del'] <> "") {
      
    /* construct and run our delete query */
      
    $query "DELETE FROM tblmembers WHERE `MemberID`='" $_GET['del'] ."' LIMIT 1";
      
    $result mysql_query ($query);
    }

    /* set the allowed order by columns */
    $default_sort 'LastName';
    $allowed_order = array ('JoinDate''FirstName','LastName''loginDateTime');

    /* if order is not set, or it is not in the allowed
    * list, then set it to a default value. Otherwise,
    * set it to what was passed in. */
    if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* construct and run our query */
    $query "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
    $result mysql_query ($query);

    /* make sure data was retrieved */
    $numrows mysql_num_rows($result);
    if (
    $numrows == 0) {

        echo 
    "No data to display!";

    } else {

      
    /* now grab the first row and start the table */
      
    $row mysql_fetch_assoc ($result);
      echo 
    "<table border=1>\n";
      
    $table_init true;
      echo 
    "<TR>\n";
      while (
    $row mysql_fetch_assoc ($result)) {
        if (
    $table_init) {
          
    /* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
          
    echo "<tr>\n";
          foreach (
    $row as $heading => $column) {
            echo 
    "<td>";
            if (
    in_array ($heading$allowed_order)) {
              echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
            } else {
              echo 
    $heading;
            }
            echo 
    "</td>\n";
          }
          echo 
    "<td>Del?</td>";
          echo 
    "</tr>\n";
        } else {
          echo 
    "<tr>\n";
          foreach (
    $row as $column) {
            echo 
    "<td>$column</td>\n";
          }
          echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?del=" $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
          echo 
    "</tr>\n";
        }
      }
      echo 
    "</table>\n";
    }
    ?>
    I didn't know the name of the member's ID field and used "MemberID" as a placeholder.

    I removed the extra loop.

  3. #3
    Join Date
    May 2003
    Location
    Australia
    Posts
    368
    Quote Originally Posted by NightShift58
    PHP Code:
    <?php
    if (isset($_GET['del']) AND $_GET['del'] <> "") {
      
    /* construct and run our delete query */
      
    $query "DELETE FROM tblmembers WHERE `MemberID`='" $_GET['del'] ."' LIMIT 1";
      
    $result mysql_query ($query);
    }

    /* set the allowed order by columns */
    $default_sort 'LastName';
    $allowed_order = array ('JoinDate''FirstName','LastName''loginDateTime');

    /* if order is not set, or it is not in the allowed
    * list, then set it to a default value. Otherwise,
    * set it to what was passed in. */
    if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* construct and run our query */
    $query "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
    $result mysql_query ($query);

    /* make sure data was retrieved */
    $numrows mysql_num_rows($result);
    if (
    $numrows == 0) {

        echo 
    "No data to display!";

    } else {

      
    /* now grab the first row and start the table */
      
    $row mysql_fetch_assoc ($result);
      echo 
    "<table border=1>\n";
      
    $table_init true;
      echo 
    "<TR>\n";
      while (
    $row mysql_fetch_assoc ($result)) {
        if (
    $table_init) {
          
    /* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
          
    echo "<tr>\n";
          foreach (
    $row as $heading => $column) {
            echo 
    "<td>";
            if (
    in_array ($heading$allowed_order)) {
              echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
            } else {
              echo 
    $heading;
            }
            echo 
    "</td>\n";
          }
          echo 
    "<td>Del?</td>";
          echo 
    "</tr>\n";
        } else {
          echo 
    "<tr>\n";
          foreach (
    $row as $column) {
            echo 
    "<td>$column</td>\n";
          }
          echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?del=" $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
          echo 
    "</tr>\n";
        }
      }
      echo 
    "</table>\n";
    }
    ?>
    I didn't know the name of the member's ID field and used "MemberID" as a placeholder.

    I removed the extra loop.
    Hi there, so if I run this is this just going to delete my database? I'm scared...

  4. #4
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by tomyknoker
    Hi there, so if I run this is this just going to delete my database? I'm scared...
    Sorry, I forgot you just wanted to deactivate the record. Instead of the delete statement, use this:
    PHP Code:
    if (isset($_GET['del']) AND $_GET['del'] <> "") {
      
    /* construct and run our delete query */
      
    $query "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" $_GET['del'] ."' LIMIT 1";
      
    $result mysql_query ($query);

    Even so, it would have only deleted on record, the one on which you would have clicked "Delete?".

  5. #5
    Join Date
    May 2003
    Location
    Australia
    Posts
    368
    Hi NightShift, I noticed you delted the exit from here was that deliberate
    PHP Code:
        echo "No data to display!";
        exit;


  6. #6
    Join Date
    May 2003
    Location
    Australia
    Posts
    368
    Hmmm second last line through out an error
    PHP Code:
      
      echo 
    "</table>\n"

    ?> 
    Any ideas?

  7. #7
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by tomyknoker
    Hi NightShift, I noticed you delted the exit from here was that deliberate
    PHP Code:
        echo "No data to display!";
        exit;

    Yes, it shouldn't be used. Use "else" instead.

    The reason for not using an exit in the middle of the script is that, if you ever wanted to add additional information at the bottom of the page - for example, a counter, a footer or maybe some debugging information, it would never get that far. I realize that, today, you know there's an exit in there. But in 2 weeks? 2 months? 2 years?

    A simple "else" takes care of that and makes your script easier to follow.

  8. #8
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by tomyknoker
    Hmmm second last line through out an error
    PHP Code:
      
      echo 
    "</table>\n"

    ?> 
    Any ideas?
    What does the error message have to say?

    Also, use the following, as I had forgotten to reset $table_init:
    PHP Code:
    <?php
    if (isset($_GET['del']) AND $_GET['del'] <> "") {
      
    /* construct and run our "deactivate" query */
      
    $query "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" $_GET['del'] ."' LIMIT 1";
      
    $result mysql_query ($query);
    }

    /* set the allowed order by columns */
    $default_sort 'LastName';
    $allowed_order = array ('JoinDate''FirstName','LastName''loginDateTime');

    /* if order is not set, or it is not in the allowed
    * list, then set it to a default value. Otherwise,
    * set it to what was passed in. */
    if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* construct and run our query */
    $query "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
    $result mysql_query ($query);

    /* make sure data was retrieved */
    if (mysql_num_rows($result)) {
      echo 
    "No data to display!";
    } else {

      
    /* now grab the first row and start the table */
      
    $row mysql_fetch_assoc ($result);
      echo 
    "<table border=1>\n";
      
    $table_init true;
      echo 
    "<TR>\n";
      while (
    $row mysql_fetch_assoc ($result)) {
        if (
    $table_init) {
          
    $table_init false;
          
    /* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
          
    echo "<tr>\n";
          foreach (
    $row as $heading => $column) {
            echo 
    "<td>";
            if (
    in_array ($heading$allowed_order)) {
              echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
            } else {
              echo 
    $heading;
            }
            echo 
    "</td>\n";
          }
          echo 
    "<td>Del?</td>";
          echo 
    "</tr>\n";
        } else {
          echo 
    "<tr>\n";
          foreach (
    $row as $heading => $column) {
            echo 
    "<td>$column</td>\n";
          }
          echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?del=" $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
          echo 
    "</tr>\n";
        }
      }
      echo 
    "</table>\n";
    }
    ?>

  9. #9
    Join Date
    May 2003
    Location
    Australia
    Posts
    368
    Hi ok it runs but doesn't give me a table. It just Outputs this when I clicked on Deleted
    There are 40 members that are DeletedNo data to display!

  10. #10
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    That's strange because the bit about "There are 40 members..." isn't in the script.

    Can you post the script as it is now?

  11. #11
    Join Date
    May 2003
    Location
    Australia
    Posts
    368
    Sorry there is a script above the code as well so maybe that's screwed it up
    PHP Code:
    <?php
    $cat 
    $_GET['cat'];

    /* connect to the mysql database and use a query to get the members info */

    include 'library/config.php';
    include 
    'library/opendb.php';

    //navigation
    include("nav.php");

    //approved
    $a mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='A'");
    $aCount mysql_num_rows($a);

    //deleted
    $d mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='D'");
    $dCount mysql_num_rows($d);

    //on hold
    $h mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='H'");
    $hCount mysql_num_rows($h);

    //pending
    $p mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='P'");
    $pCount mysql_num_rows($p);

    //not sure
    $n mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='N'");
    $nCount mysql_num_rows($n);

    //rejected
    $r mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='R'");
    $rCount mysql_num_rows($r);

    if (
    $cat == "a") {
    $field "Approved";
    $theCount $aCount;
    }

    elseif (
    $cat == "d") {
    $field "Deleted";
    $theCount $hCount;
    }

    elseif (
    $cat == "h") {
    $field "On Hold";
    $theCount $hCount;
    }

    elseif (
    $cat == "p") {
    $field "Pending";
    $theCount $pCount;
    }

    elseif (
    $cat == "n") {
    $field "Not Sure";
    $theCount $nCount;
    }

    elseif (
    $cat == "r") {
    $field "Rejected";
    $theCount $rCount;
    }

    echo 
    'There are '.$theCount.' members that are '.$field;


    if (isset(
    $_GET['del']) AND $_GET['del'] <> "") { 
      
    /* construct and run our "deactivate" query */ 
      
    $query "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" $_GET['del'] ."' LIMIT 1"
      
    $result mysql_query ($query); 


    /* set the allowed order by columns */ 
    $default_sort 'LastName'
    $allowed_order = array ('JoinDate''FirstName','LastName''loginDateTime'); 

    /* if order is not set, or it is not in the allowed 
    * list, then set it to a default value. Otherwise, 
    * set it to what was passed in. */ 
    if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) { 
        
    $order $default_sort
    } else { 
        
    $order $_GET['order']; 


    /* construct and run our query */ 
    $query "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order"
    $result mysql_query ($query); 

    /* make sure data was retrieved */ 
    if (mysql_num_rows($result)) { 
      echo 
    "No data to display!"
    } else { 

      
    /* now grab the first row and start the table */ 
      
    $row mysql_fetch_assoc ($result); 
      echo 
    "<table border=1>\n"
      
    $table_init true
      echo 
    "<TR>\n"
      while (
    $row mysql_fetch_assoc ($result)) { 
        if (
    $table_init) { 
          
    $table_init false
          
    /* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */ 
          
    echo "<tr>\n"
          foreach (
    $row as $heading => $column) { 
            echo 
    "<td>"
            if (
    in_array ($heading$allowed_order)) { 
              echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>"
            } else { 
              echo 
    $heading
            } 
            echo 
    "</td>\n"
          } 
          echo 
    "<td>Del?</td>"
          echo 
    "</tr>\n"
        } else { 
          echo 
    "<tr>\n"
          foreach (
    $row as $heading => $column) { 
            echo 
    "<td>$column</td>\n"
          } 
          echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?del=" $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>"
          echo 
    "</tr>\n"
        } 
      } 
      echo 
    "</table>\n"

    ?>

  12. #12
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    I had also forgotten to remove a "<TR>", which may account for the table not displaying. Try this:
    PHP Code:
    <?php
    $cat 
    $_GET['cat'];

    /* connect to the mysql database and use a query to get the members info */

    include 'library/config.php';
    include 
    'library/opendb.php';

    //navigation
    include("nav.php");

    //approved
    $sql mysql_query("
          SELECT 'Approved' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='A'
    UNION SELECT 'Deleted'  as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='D'
    UNION SELECT 'On Hold'  as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='H'
    UNION SELECT 'Pending'  as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='P'
    UNION SELECT 'Not Sure' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='N'
    UNION SELECT 'Rejected' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='R'
    "
    ;
    $qry mysql_query($sql) or die("SQL Error: $sql<br>" mysql_error());

    echo 
    "Overview:<br>";
    WHILE (
    $r mysql_fetch_array($qry)) :
      echo 
    $r['MemberApp'] . ": " $r['cnt'] . "<br>";
    ENDWHILE;

    if (isset(
    $_GET['del']) AND $_GET['del'] <> "") {
      
    /* construct and run our "deactivate" query */
      
    $query "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" $_GET['del'] ."' LIMIT 1";
      
    $result mysql_query ($query);
    }

    /* set the allowed order by columns */
    $default_sort 'LastName';
    $allowed_order = array ('JoinDate''FirstName','LastName''loginDateTime');

    /* if order is not set, or it is not in the allowed
    * list, then set it to a default value. Otherwise,
    * set it to what was passed in. */
    if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* construct and run our query */
    $query "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
    $result mysql_query ($query);

    /* make sure data was retrieved */
    if (mysql_num_rows($result)) {
      echo 
    "No data to display!";
    } else {

      
    /* now grab the first row and start the table */
      
    $row mysql_fetch_assoc ($result);
      echo 
    "<table border=1>\n";
      
    $table_init true;
      while (
    $row mysql_fetch_assoc ($result)) {
        if (
    $table_init) {
          
    $table_init false;
          
    /* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
          
    echo "<tr>\n";
          foreach (
    $row as $heading => $column) {
            echo 
    "<td>";
            if (
    in_array ($heading$allowed_order)) {
              echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
            } else {
              echo 
    $heading;
            }
            echo 
    "</td>\n";
          }
          echo 
    "<td>Del?</td>";
          echo 
    "</tr>\n";
        } else {
          echo 
    "<tr>\n";
          foreach (
    $row as $heading => $column) {
            echo 
    "<td>$column</td>\n";
          }
          echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?del=" $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
          echo 
    "</tr>\n";
        }
      }
      echo 
    "</table>\n";
    }
    ?>

Thread Information

Users Browsing this Thread

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

     

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
HTML5 Development Center



Recent Articles