dqyat62284 2016-11-11 22:26 采纳率: 0%
浏览 40
已采纳

允许用户删除其条目

I have this code which builds a table based on SQL date from the user (their posts):

<table>
             <?php
$table_name =  "candle_number";
$data = $wpdb->get_results( "SELECT * FROM `{$table_name}` WHERE `userid` = '{$current_user->ID}';" );
$i=1;/* to what purpose?? */
foreach( $data as $rs ){
    echo "
    <tr>
        <td><strong>{$rs->dname}</strong></td>
        <td>{$rs->dateenglish}<br>{$rs->datehebrew}</td>
        <td><!-- /* Notice the various dataset attributes here */ -->
            <a href='#' data-name='{$rs->dname}' data-dod='{$rs->dateenglish}' data-hdod='{$rs->datehebrew}'>
                <img width=50 height=50 src='http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/bootstrap/images/Candle01.gif' class='img-responsive candle' alt='Yahrzeit Candle' style='margin:auto !important;' />
            </a><br>
            <p style='text-align:center !important; margin:-10px 0 0 0 !important; padding:0; line-height:7px !important;'><span style='color: #f26522 !important; font-size:10px !important; font-weight:700 !important;'>CLICK TO LIGHT</span></p>
        </td>
        <td><a href='delete.php?id=$id'>Delete</a></td>
    </tr>";
    $i++;
}
?>
</table>

which has an href to delete.php:

<?php

$id = $_GET['id'];

$sql_ = "DELETE FROM msparks_wp3.candle_number WHERE id = '$id'";
mysqli_query($dbc, $query) or die('Database error!');
header('location:MYURL');
?>

And the last TD displays a "DELETE" url that is supposed to remove that specific row - but it is not working. I was thinking of using a different method, but I cannot find the right way to make it NOT delete all of the table contents.

Am I missing a step?

  • 写回答

1条回答 默认 最新

  • dongzhuner6981 2016-11-14 20:10
    关注

    This was rather interesting. I used a combination of $wpdb function and basic SQL:

    <?php
    //WP Stuff
    define('WP_USE_THEMES', false);
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/digital-yahrzeit/wp-load.php' );
    
    $servername = "localhost";
    $username = "USERNAME";
    $password = "PASSWORD";
    $dbname = "DB_NAME";
    
    // Create connection
    $conn = mysql_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysql_error());
    }
    
    // sql to delete a record
    $id = $_GET['id'];
    
    $id = filter_input( INPUT_GET, 'id', FILTER_SANITIZE_STRING );
    $table_name =  "candle_number";
    $wpdb->query( "DELETE FROM `{$table_name}` WHERE `id` = '$id'" );
    
    mysql_close($conn);
    
    header('location:MY_HEADER');
    ?>
    

    Thank you all who answered.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?