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.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts

    PHP Checkboxes & mySQL Database

    Hey all;

    I am working on a project where I have a database of a list of tasks that I have to develop a PHP application that allows for adding, viewing, and deleting items from the database. To delete items, I have them in a checkbox list, and the user must check the box next to the item they wish to remove from the list, and press submit. Here is my PHP code:

    Code:
    <?php
    	
    	switch($_GET['state']) {
    
    		case "add": if(isset($_POST['addtask'])) {
    			if(empty($_POST['task'])) {
    				$error['task'] = 'A task is required.';
    			}
    
    			if(sizeof($error) == 0) {
    				$sql = "INSERT INTO tasks (
    								task_id, 
    								taskname
    							) VALUES (
    								null, 
    								'{$_POST['task']}'
    							)";
    				mysql_query($sql);
    		 
    				echo "<p>New task entered.</p>";
    			} 
    		}	
            break;
    		
    		case "delete": if(isset($_POST['submittask'])) {
    		
    			$taskentry = 'unchecked';
    			
    			if(isset($_POST['taskentry']) == true) {
    				
    				$sql = "DELETE FROM tasks WHERE task_id = '{$_GET['taskentry']}'";
    				mysql_query($sql);
    				echo "<p>Your post has been deleted.</p>";
    						
    			} 
    		}
    	}
    		
    ?>
    
    	<form method="post" action="apps.php?state=add">
        	<label class="php">New Task</label>
            <input type="text" name="task" /><br />
            <?php echo "<p>";
    			  echo $error['task'];
    			  echo "</p>";
    		?>
            <input type="submit" name="addtask" value="Add" />
        </form>
        
        <form metod="post" action="apps.php?state=delete">
            <?php
                $sql = "SELECT 
                            task_id,
                            taskname
                        FROM 
                            tasks
                        ORDER BY 
                            task_id";
                $result = mysql_query($sql);
                while($row = mysql_fetch_array($result, MYSQL_ASSOC))
                {
    				echo "<font class='php'>";
    				echo "<input name='taskentry' type='checkbox'>{$row['taskname']}</input><br />";
    				echo "</font>";
    			}
    		?><br /><br />
            <input type="submit" name="submittask" value="Submit" />
        </form>
    I can't quite figure out how to use the checkboxes in PHP, and so far the only way I have been able to remove items from the list is if I remove them all at once. Can you help me figure out how to properly delete items that are checked on this list from the database? Thank you and please let me know if you have any questions.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    You need to make a checkbox array with values ...

    echo "<input name='taskentry[]' type='checkbox' value='{$row['task_id']}'>{$row['taskname']}</input><br />";


    Now, when a checkbox is checked, the array called "taskentry[]" will have
    values in them. The array will only contain the ones that were checked.


    I can't test this part so make a safe copy before you overwrite it ...

    PHP Code:

    case "delete": if(isset($_POST['submittask'])) {
            
                
    //$taskentry = 'unchecked';
                
                //if(isset($_POST['taskentry']) == true) {

    $tasks=$_POST['taskentry'];
    foreach(
    $tasks as $task){
                    
                    
    $sql "DELETE FROM tasks WHERE task_id = '$task'";
                    
    mysql_query($sql);
                    echo 
    "<p>Your post has been deleted.</p>";
                            
                } 
            } 

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hello mlseim;

    Thank you for your response! Unfortunately, the code you provides did not work. I'm still a little confused as to how this works. I added the array like you suggested, but where do I go from there? I want to be able to check one or multiple tasks and click "submit", removing the tasks from the database. Like I said before, the only way I got it to work was by deleting all of the tasks at once, which I thought was because I contained all of the tasks in one line of code. Arrays confuse me a little, if you have any suggestions or I am looking at the problem the wrong way, please let me know.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Does your script look like the one below ...
    This is how it should look after you entered my changes ...
    Let me know if this is failing, and what you mean by "doesn't work".


    PHP Code:

    <?php
        
        
    switch($_GET['state']) {

            case 
    "add": if(isset($_POST['addtask'])) {
                if(empty(
    $_POST['task'])) {
                    
    $error['task'] = 'A task is required.';
                }

                if(
    sizeof($error) == 0) {
                    
    $sql "INSERT INTO tasks (
                                    task_id, 
                                    taskname
                                ) VALUES (
                                    null, 
                                    '{$_POST['task']}'
                                )"
    ;
                    
    mysql_query($sql);
             
                    echo 
    "<p>New task entered.</p>";
                } 
            }    
            break;
            
    case 
    "delete": if(isset($_POST['submittask'])) { 
             
                
    //$taskentry = 'unchecked'; 
                 
                //if(isset($_POST['taskentry']) == true) { 

    $tasks=$_POST['taskentry']; 
    foreach(
    $tasks as $task){ 
                     
                    
    $sql "DELETE FROM tasks WHERE task_id = '$task'"
                    
    mysql_query($sql); 
                    echo 
    "<p>Your post has been deleted.</p>"
                             
                }  
            }  
    }        
    ?>

        <form method="post" action="apps.php?state=add">
            <label class="php">New Task</label>
            <input type="text" name="task" /><br />
            <?php echo "<p>";
                  echo 
    $error['task'];
                  echo 
    "</p>";
            
    ?>
            <input type="submit" name="addtask" value="Add" />
        </form>
        
        <form metod="post" action="apps.php?state=delete">
            <?php
                $sql 
    "SELECT 
                            task_id,
                            taskname
                        FROM 
                            tasks
                        ORDER BY 
                            task_id"
    ;
                
    $result mysql_query($sql);
                while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
                {
                    echo 
    "<font class='php'>";
                    echo 
    "<input name='taskentry[]' type='checkbox' value='{$row['task_id']}'>{$row['taskname']}</input><br />";
                    echo 
    "</font>";
                }
            
    ?><br /><br />
            <input type="submit" name="submittask" value="Submit" />
        </form>
    Last edited by mlseim; 02-27-2012 at 03:46 AM.

  • Users who have thanked mlseim for this post:

    Myrmidon16 (02-27-2012)

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    mlseim, my code looks like yours, however the application is still not deleting the checked tasks from the database. I check the task's checkbox, click submit, and the page reloads, however the task is not removed and the echo "Your post has been deleted" doesn't appear. Here is my code, if I missed anything:
    PHP Code:
    <?php
        
        
    switch($_GET['state']) {

            case 
    "add": if(isset($_POST['addtask'])) {
                if(empty(
    $_POST['task'])) {
                    
    $error['task'] = 'A task is required.';
                }

                if(
    sizeof($error) == 0) {
                    
    $sql "INSERT INTO tasks (
                                    task_id, 
                                    taskname
                                ) VALUES (
                                    null, 
                                    '{$_POST['task']}'
                                )"
    ;
                    
    mysql_query($sql);
             
                    echo 
    "<p>New task entered.</p>";
                } 
            }    
            break;
            
            case 
    "delete": if(isset($_POST['submittask'])) { 

                
    $tasks=$_POST['taskentry']; 
                foreach(
    $tasks as $task){
                    
    $sql "DELETE FROM tasks WHERE task_id = '$task'"
                    
    mysql_query($sql); 
                    echo 
    "<p>Your post has been deleted.</p>";          
                }  
            }  
        }
            
    ?>

        <form method="post" action="apps.php?state=add">
            <label class="php">New Task</label>
            <input type="text" name="task" /><br />
            <?php echo "<p>";
                  echo 
    $error['task'];
                  echo 
    "</p>";
            
    ?>
            <input type="submit" name="addtask" value="Add" />
        </form>
        
        <form metod="post" action="apps.php?state=delete">
            <?php
                $sql 
    "SELECT 
                            task_id,
                            taskname
                        FROM 
                            tasks
                        ORDER BY 
                            task_id"
    ;
                
    $result mysql_query($sql);
                while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
                {
                    echo 
    "<font class='php'>";
                    echo 
    "<input name='taskentry[]' type='checkbox' value='{$row['task_id']}'>{$row['taskname']}</input><br />";
                    echo 
    "</font>";
                }
            
    ?><br /><br />
            <input type="submit" name="submittask" value="Submit" />
        </form>
    Also, here is the site where I am running the application, if you need to look at it in process in order to figure out what is going wrong: http://myrmidon16.x10.mx/sideQUEST/apps.php. I'll keep researching this, as I am relatively new to PHP, but if you can figure anything else, please let me know. Thank you.

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Alright, I misspelled "method" in my form. It works now, thank you so much for your help. I hate PHP.

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    no problem ... glad you found that typo.

    I wasn't sure where I was going to go next.


  •  

    Posting Permissions

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