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 18
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question problems deleting from database

    hi i am fairly new to php/mysql i have this program that does not want to delete from the database. at first it was telling me that the file had been delete and when i checked the database it was still there. no i get two different errors depending on what i do to the code.
    the delete code
    Code:
    <?PHP
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    include 'mysqlconnectsitel.inc.php';// this has my database connection stuff
    
    if($_GET['m'] == 'del' )// one error i get is right here
    {
    
    $requestId = $_GET['id'];
    //$last=$_GET['last'];
    //$first=$_GET['first'];
    $query ="DELETE FROM timeoff WHERE requestId = '$requestId' ";// if i change this to last ='$last' AND first ='$first' o get undefined variables not pulling the data
    
    $result= mysql_query($query) or die(mysql_error());
    
    echo" <center><font color=\"#000000\">Your Info Was Deleted!</font></center> ";
    echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
    echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
    
    }
    else{
    
    echo "No selection was made. please try again.";
     
    
    echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
    echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
    }
     ?>
    this code is activated from a link on a page that has check boxes and a link for delete that uses this. please help have been trying for a few days to get this.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    What are the errors you're getting?

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question

    the errors are the following


    Notice: Undefined index: m in C:\apache2triad\htdocs\index.php on line 7 No selection was made. please try again.

    i did have something selected

    right now cause i just tried it, the above error is all that i am getting


    i fixed the error i took that out a replaced it with something else, but now when i run it a check a ckeck box hit delete link it say no selection was made.
    Last edited by amylou111770; 08-30-2007 at 12:15 AM. Reason: fixed that error

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Are you POST'ing the form? <form method="post"
    If so, you should be using $_POST instead of $_GET.
    If not, your variable isn't being passed in the URL or isn't the correct name.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    here is the code that does the sql query and shows the results with check boxes. the delete is a link

    Code:
    if(isset($_POST['action'])){$action=$_POST['action'];}else{$action="";}
    if( $action == 'updated' )
    {
      extract ($_POST);
      $todaysDate = date("Y-m-d",time());
      
            
    
    //	Need to query database to see if this request is valid
    
    	
    	echo "<p><strong>These are the dates that you have requested off!!!!</strong></p>";
    	
    	
    	$sql ="SELECT first, last, requestedDate FROM timeoff WHERE last = '$last' and first = '$first' ";
    $query=mysql_query($sql) or die("Query failed : " . mysql_error());
    while($row = mysql_fetch_assoc($query)) {
    	echo "<p>";
    	echo $row['first'] ." ". $row['last']. " ". "  ". $row['requestedDate'];	
    	echo "<td><input type=\"checkbox\" name=\"deleted_items[]\" "; 
    	echo "</p>";
    }
            
     $action="";
     
     $_html ="<tr> 
        <td>$_rw->requestId</td>
        <td>$_rw->first</td>
    	<td>$_rw->last</td>
    	<td>$_rw->requestedDate</td>
         <td>
      		 <a href=\"index.php?id\">DELETE </a></td>
    
        </tr>";
    	echo $_html;
    	
    
    }
    else
         
    {
      
      
    ?>
    here is the link for the delete code
    Code:
     td>
      		 <a href=\"index.php?id\">DELETE </a></td>
    is there a tutitorial that shows how to delete this way with check boxes?

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Well, that delete link isn't going to do anything because it's just passing "id" as the query string. If you want to use checkboxes you have to provide each with the id of the MySQL row you want to remove. ie.
    PHP Code:
    echo '<td><input type="checkbox" name="deleted_items[' $row['id'] . ']" />'// just noticed you didn't close your tag either 
    And loop through them on the next page. $_POST['deleted_items'] should be an array of the checkboxes that were checked(or nothing if none were checked).
    PHP Code:
    if(!empty($_POST['deleted_items']))
    {
        foreach(
    $_POST['deleted_items'] as $id => $on)
        {
            
    $query 'DELETE FROM `table` WHERE `id` = ' $id;
            
    // mysql_query()
        
    }


  • #7
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i tried that and its still telling me that no selection is made when i did.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Your inputs need to be wrapped in:
    Code:
    <form action="page.php?m=del" method="post">
    
    <!-- checkbox inputs here -->
    
    <input type="submit" value="Delete" />
    </form>
    So the while loop would look something like this:
    PHP Code:
    <?php
    // code...
    ?>
    <form action="page.php?m=del" method="post"> <!-- <-- change the page name.. -->
    <?php

    while($row mysql_fetch_assoc($query))
    {
        echo 
    '<p>' $row['first'] . ' ' $row['last'] . ' ' $row['requestedDate'];    
        echo 
    '<td><input type="checkbox" name="deleted_items[' $row['id'] . ']" />'// I put "id" here, you need to change this to your id field, and make sure you select it in your query 
        
    echo '</p>';
    }
    ?>

    <!-- checkbox inputs here -->

    <input type="submit" value="Delete" />
    </form>
    <?php
    // code...
    ?>
    Also, whenever you make changes, please post the code so we can see what the latest version looks like, and be able to spot errors easier.

  • #9
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    can i use this even though the delete code is on a different page then the code for the check boxes

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Yes, as long as the delete page is set in the <form action=""

  • #11
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it now tells me that it has been deleted but still not taking it out of the database i have changed what needed to be changed. any other ideas

    thank you for all your help

  • #12
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Try echo'ing mysql_affected_rows() and $query:
    PHP Code:
    $query ''// your query

    echo $query '<br />';
    $result mysql_query($query) or die(mysql_error());
    echo 
    'Records deleted: ' mysql_affected_rows() . '<br />'
    Post both here, and try to run the query in phpMyAdmin to see if you get any errors.

  • Users who have thanked Inigoesdr for this post:

    amylou111770 (08-30-2007)

  • #13
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    DELETE FROM `timeoff` WHERE requestId = 0
    Records deleted: 0

    this is what i got when i echoed the query and the mysql_affected_rows

  • #14
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    and if i select all of them to delelte the
    requestId = 0
    becomes a 3 and the records deleted stays the same

  • #15
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Ok, so $requestId isn't being set on the delete page. Did you make that loop I posted earlier? Post the delete page.

    Also, look at the source of the page with the checkboxes and see if there are numbers in the name="deleted_items[#]". Post that page too if there aren't any numbers. If there are numbers then all we need to fix is the loop on the delete page.


  •  
    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
    •