SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete from Database by specific ID

    Not sure how to do this, i tried a few things with no success

    I want to be able to delete individual or several tables with a check box

    this is my code

    PHP Code:
    <?php
    // php extension using
    $phpEx substr(strrchr(__FILE__'.'), 1);

    if ( !
    defined('IN_PHPBB') ) 
    {
        
    define('IN_PHPBB'true);
    }

    include(
    $phpbb_root_path  'common.' $phpEx);
    include(
    $phpbb_root_path 'includes/bbcode.' $phpEx);

    $user->session_begin();
    $auth->acl($user->data);
    $user->setup();


    // connection

    // (...)


    /* process post data */
    if(!empty($_POST)) {
        
    $sql 'INSERT INTO bikes (bike_name,bike_photo,bike_link, bike_specs, date) VALUES ("'.mysql_real_escape_string($_POST['bike_name']).'","'.mysql_real_escape_string($_POST['bike_photo']).'","'.mysql_real_escape_string($_POST['bike_link']).'","'.mysql_real_escape_string($_POST['bike_specs']).'","'.mysql_real_escape_string($_POST['date']).'");';
        
    mysql_query($sql) or die(mysql_error());
    }

    $previousBikeId mysql_insert_id();
    $previousBike = array();
    $previousMatch false;
    $bikes = array();

    $sql 'SELECT id,bike_name,bike_link,bike_photo,date FROM bikes ORDER BY id DESC;';
    $result mysql_query($sql);

    while(
    $row mysql_fetch_assoc($result)) {
        if(!
    $previousMatch && $previousBikeId==$row['id'] ) {
            
    /* separate previous bike from others */
            
    $previousBike $row;
            
    $previousMatch true;
        } else {
            
    $bikes[] = $row;
        }
    }

    /* start view */
    ?>
    <html>
    <head>
    <title>Admin bow controls</title>
    <style type="text/css">
    <!--
    .bold {
        font-weight: bold;
    }
    -->
    </style>
    </head>
    <body>
    <?php
      
    if ($user->data['is_registered'] && ($auth->acl_getf_global('m_') || $auth->acl_get('a_')))
        {
        
    ?>
    <?php
    if(!empty($previousBike)) { ?>
        <p>You just did this:</p>
    <ul>
            <li><strong>Bike Name</strong>:&nbsp;<?php echo htmlentities($previousBike['bike_name']); ?></li>
            <li><strong>Bike Photo</strong>:&nbsp;<?php echo htmlentities($previousBike['bike_photo']); ?></li>
            <li><strong>Bike Link</strong>:&nbsp;<?php echo htmlentities($previousBike['bike_link']); ?></li>
            <li><strong>Date</strong>:&nbsp;<?php echo htmlentities($previousBike['Date']); ?></li>
        </ul>
    <p>
      <?php 
    // want a delete check box in here

    if(!empty($bikes)) {
        echo 
    '<table border="1">';
        echo 
    '<thead>';
        echo 
    '<th>Bike Name</th>';
        echo 
    '<th>Bike Photo</th>';
        echo 
    '<th>Bike Link</th>';
        echo 
    '<th>date</th>';
        echo 
    '</thead>';
        echo 
    '<tbody>';
        foreach(
    $bikes as $bike) {
            echo 
    '<tr>';
            echo 
    '<td>',htmlentities($bike['bike_name']),'</td>';
            echo 
    '<td>',htmlentities($bike['bike_photo']),'</td>';
            echo 
    '<td>',htmlentities($bike['bike_link']),'</td>';
            echo 
    '<td>',htmlentities($bike['date']),'</td>';
            echo 
    '</tr>';
        }
        echo 
    '<tbody>';
        echo 
    '</table>';
    }
    ?>
    <p class="bold">do not refresh this page, it may duplicate the data. </p>
    <p class="bold"><a href="http://www.street-steeze.com">go to homepage to make sure it worked</a></p>
    <p class="bold"><a href="http://www.street-steeze.com/bikeow.php">go to previous bike of the week to make sure it also worked</a></p>
    <p class="bold"><a href="http://www.street-steeze.com/index.php">go to forum</a></p>
      <?php
        
    }
        else{
            echo 
    "You cannot view this page";
        }
        
    ?>
          
    </p>
    <p class="bold"><a href="http://www.street-steeze.com/index.php">go to forum</a></p>

    </body>
    </html>
    Help is appreciated

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If I have understood correctly you want a checkbox next to each bike name, in the table of all bikes.

    If so, you need to:

    1 add another column to your html table.

    <td>Delete?</td>

    2 add a checkbox which is unchecked next to each bike ...

    echo "<td><input type=checkbox name=\"delBike[$bike['id']]\" ></td>";

    This contains a readymade array called delBike, so it will contain
    delBike[1]
    delBike[2]
    delBike[3]

    check the html of your html form carefully, look at source code to make sure the quoting doesnt get screwed.

    3 on the backend, if $delBike isarray ( ie at least one has been clicked), then loop through the array deleting the bikes as you go.

    foreach( $_POST['delBike'] as $k=>$v ){

    $sql = "delete from bikes where id=$k limit 1 ";

    }

    I think thats it. Personally I'd get a JS confirm() message to make sure that the user understands what they are doing, or, dont actually delete each bike, set a flag in your database to display = 0 when someone deletes a bike.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help!

    i am getting this error on line 94

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
    line 94

    PHP Code:
    echo "<td><input type=checkbox name=\"delBike[$bike['id']]\" ></td>"

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/types.string

    See the variable parsing section for proper syntax methods.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the best i can figure outb ut its probably completly wrong

    PHP Code:
            echo '<td>''<input type=checkbox name=' delBike[$bike['id']] '>' '</td>'

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sorry, I just checked your original quoting convention again, so try

    echo '<td><input type="checkbox" name="delBike['. $bike['id'] . ']" ></td>';

    Sorry, 'bout that, just went for medinner.

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks i have the check boxes in place now and are working correctly with the rite ID but how would i get the submit button to work?

    Do i have another file with this in it?

    PHP Code:
    foreach( $_POST['delBike'] as $k=>$v ){

    $sql "delete from bikes where id=$k limit 1 ";


    and link the submit button to that file?

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    yes.

  9. #9
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a side note: rather than performing a query for each database row (which is time-consuming):

    PHP Code:
    foreach( $_POST['delBike'] as $k=>$v ){

    $sql "delete from bikes where id=$k limit 1 ";


    You can delete all rows in one go:

    PHP Code:
    $delete_ids implode(','array_keys($_POST['delBike']));
    $sql "DELETE FROM `bikes` WHERE `id` IN ($delete_ids)"
    Of course after sanitazing $_POST['delBike'] array values.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Of course, very nice.

    Still, it seems the OP is facing more fundamental issues ...

  11. #11
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed. I doubted for a sec if I should post it but I thought it's important to learn the right way the first time. I hope it doesn't confuse the OP.


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
  •