We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26182
    • 164 Posts
    hi,

    I was just wondering if anyone would be able to help me with this code:

    <?php
    // deletestudents.php - a customised php page that works from a snippet call to list web users in the
    // database and provide functionality for the students to be deleted from the database
    // author: sarah vardy

    // Establish a connection to the MySQL DBMS
    $connection = mysql_connect("localhost", "root", "s0laspie");

    // Use the modx database
    mysql_select_db("modx", $connection);

    // Run a query through the connection
    $result = mysql_query("SELECT id, username FROM modx_web_users", $connection);

    // Fetch each row of the results into an array $row
    while ($row = mysql_fetch_array($result))
    {
    echo "{$row["username"]}\n\n";?><input name="checked" type="checkbox" value="id">

    I am trying to pass the value from the checkbox to delete entries from the database. How do I achieve this or is there another way of doing it? Maybe with a dynamic drop down list or something.

    Any help or suggestions anyone can provide would be greatly appreciated.

    Thankyou.

    Sarah
      • 26182
      • 164 Posts
      I’ve changed the code to this:

      <?php
      // deletestudents.php - a customised php page that works from a snippet call to list web users in the
      // database and provide functionality for the students to be deleted from the database
      // author: sarah vardy
      
      // Establish a connection to the MySQL DBMS
      $connection = mysql_connect("localhost", "root", "s0laspie");
      
      // Use the modx database
      mysql_select_db("modx", $connection);
      
      
      
      //If cmd has not been initialized
      if(!isset($cmd)) 
      {
         // Run a query through the connection
         $result = mysql_query("SELECT id, username FROM modx_web_users ORDER BY id", $connection);
         
         		//run the while loop that grabs all the news scripts
        	 	while($r=mysql_fetch_array($result)) 
         		{ 
            		//grab the title and the ID of the news
            		$username=$r["username"];//take out the title
            		$id=$r["id"];//take out the id
           
      	 		//make the title a link
            		echo "<a href='index.php?id=44?cmd=delete&id=$id'>$username - Delete</a>";
           		echo "<br>";
          	}
      	}
      
          if($_GET["cmd"]=="delete")
      	{
          	$sql = "DELETE FROM modx_web_users WHERE id=$id";
          	$result = mysql_query($sql);
          	echo "Row deleted!";
      	}
      ?>


      but it doesn’t seem to want to delete the entries in the database. Does anyone know what I could be doing wrong?

      Thanks very much.

      Sarah
        • 28042 ☆ A M B ☆
        • 24,524 Posts
            	$sql = "DELETE FROM modx_web_users WHERE id=$id";

        Maybe this would work if it were
            	$sql = "DELETE FROM modx_web_users WHERE id=$_GET['id']";
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 26182
          • 164 Posts
          Thanks for the assistance but unfortunately it’s still not working. Nothing is showing on the page now at all. Is there something else I could be missing? I really appreciate your help smiley


          <?php
          // deletestudents.php - a customised php page that works from a snippet call to list web users in the
          // database and provide functionality for the students to be deleted from the database
          // author: sarah vardy
          
          // Establish a connection to the MySQL DBMS
          $connection = mysql_connect("localhost", "root", "s0laspie");
          
          // Use the modx database
          mysql_select_db("modx", $connection);
          
          
          
          //If cmd has not been initialized
          if(!isset($cmd)) 
          {
             // Run a query through the connection
             $result = mysql_query("SELECT id, username FROM modx_web_users ORDER BY id", $connection);
             
             		//run the while loop that grabs all the news scripts
            	 	while($r=mysql_fetch_array($result)) 
             		{ 
                		//grab the title and the ID of the news
                		$username=$r["username"];//take out the title
                		$id=$r["id"];//take out the id
               
          	 		//make the title a link
                		echo "<a href='index.php?id=44?cmd=delete&id=$id'>$username - Delete</a>";
               		echo "<br>";
              	}
          	}
          
                  if($_GET["cmd"]=="delete")
          	{
          		echo $_GET['id'];
          
              	//$sql = "DELETE FROM modx_web_users WHERE id=$id";
          
          	$sql = "DELETE FROM modx_web_users WHERE id=" . $_GET['id'];
              	$result = mysql_query($sql);
              	echo "Row deleted!";
          	}
          ?>
          
            • 28042 ☆ A M B ☆
            • 24,524 Posts
                  		echo "<a href='index.php?id=44?cmd=delete&id=$id'>$username - Delete</a>";

            Notice that you have double ? in the query string. If you are using friendly URLS, ?cmd=delete is OK, but if you aren’t it has to be &cmd=delete.

            Best is to create a variable based on the site’s configuration (using FURLs or not) and use that instead of ? or & for the cmd key.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: sottwell at Feb 07, 2007, 07:01 AM

                    		echo "<a href='index.php?id=44?cmd=delete&id=$id'>$username - Delete</a>";

              Notice that you have double ? in the query string. If you are using friendly URLS, ?cmd=delete is OK, but if you aren’t it has to be &cmd=delete.

              Best is to create a variable based on the site’s configuration (using FURLs or not) and use that instead of ? or & for the cmd key.
              It’s better to use $modx->makeUrl(44, ’’, ’cmd=delete’); and you cannot use id= or q= in your query string for passing the record id you want to delete; id and q are used by MODx and will mess up it’s ability to direct you to the proper page.
                • 26182
                • 164 Posts
                is the problem here that you can’t access and modify the table through modx with the mysql get command? it is now going to the correct page but nothing is deleted from the database. Any help, information or suggestions are welcomed wink

                <table border="2" cellspacing="2" cellpadding="2" width="50%">
                <tr>
                <th><font face="Arial, Helvetica, sans-serif">Name</font></th>
                </tr>
                <?php
                // deletestudents.php - a customised php page that works from a snippet call to list web users in the
                // database and provide functionality for the students to be deleted from the database
                // author: sarah vardy
                
                // Establish a connection to the MySQL DBMS
                $connection = mysql_connect("localhost", "root", "s0laspie");
                
                // Use the modx database
                mysql_select_db("modx", $connection);
                
                
                
                //If cmd has not been initialized
                if(!isset($cmd)) 
                {
                   // Run a query through the connection
                   $result = mysql_query("SELECT id, username FROM modx_web_users ORDER BY id", $connection);
                   
                   		//run the while loop that grabs all the users
                  	 	while($r=mysql_fetch_array($result)) 
                   		{ 
                      		//grab the title and the ID of the user
                      		$username=$r["username"];//take out the title
                      		$Uid=$r["id"];//take out the id
                     ?>
                	 <tr><td>
                	 <?php
                	 		$url = $modx->makeUrl(44, '', "cmd=delete&Uid=$Uid");
                			//make the title a link
                			echo "<a href='$url'>$username - Delete</a>"; 
                     		?>
                			</td>
                			</tr>
                			<?php
                    	}
                		if($_GET["cmd"]=="delete")
                		{
                		assert (false);
                		echo $_GET['Uid'];
                		$sql = "DELETE FROM modx_web_users WHERE id=" . $_GET['Uid'];
                    	$result = mysql_query($sql);
                    	echo "Row deleted!";
                		}
                	}
                
                ?>
                
                <?
                echo "</table>";
                ?>


                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  It looks to me like you are making this a lot harder than it needs to be. If you are including this .php file in a snippet, then the database connection is already made.

                  If I am reading your code correctly, what you are doing is checking for the cmd value in the URL. If it isn’t there, you generate a list of web users as a link with the cmd=delete&Uid=xx. If it is there, you want to delete the given user.

                  You can use the PHP $_GET array to deal with the URL query string. MySQL does not have anything to do with that. You use PHP to generate a query that MySQL can understand.

                  Here’s how I would do it (as a snippet):

                  if(isset($_GET['cmd']) && isset($_GET['Uid']) && $_GET['cmd'] == 'delete' && is_numeric($_GET['Uid'])) { // always validate everything 
                      $from = $modx->getFullTableName('web_users');
                      $where = "id=".$_GET['Uid'];
                      $modx->db->delete($from,$where); // see manager/includes/extenders/dbapi.mysql.class.inc.php
                      $url = $modx->makeUrl($modx->documentIdentifier);
                      $modx->sendRedirect($url); // reload the original page after deleting
                  } else {
                      $links = '<table cellspacing="0" cellpadding="0" id="listTable"><tr><th>Name</th></tr>'; // style with CSS
                      $from = $modx->getFullTableName('web_users');
                      $result = $modx->db->select("id,username", $from);
                      while($row = $modx->db->getRow($result)) {
                          $url = $modx->makeUrl($modx->documentIdentifier,'','cmd=delete&Uid='.$row['id']);
                          $links .= '<tr><td><a href="'.$url.'">'.$row['username'].' - Delete</a></td></tr>';
                      }
                      $links .= '</table>';
                  }
                  
                  return $links;
                  


                  See http://www.sottwell.com/delete-users.html for this in action. Don’t be afraid to delete; these are just testing the RegisterX snippet.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 26182
                    • 164 Posts
                    hi sottwell,

                    thanks for your help. i am going to try out the method you suggested and see if i have any success. i tried to visit the web link you suggested and it did not display anything.

                    thankyou.

                    :) sarah
                      • 26182
                      • 164 Posts
                      thanks so much sottwell got it working! grin