在这里,我基于当前登录的用户创建了一个自定义概要文件页面,以便只检查插入自定义数据库中的自己的数据。
您可以看到,在每一行中,我添加了一个缺少相应代码的按钮“Delete”。
除了我想添加一个函数来删除记录的那部分之外,一切都很好。也许$wpdb->delete()可以解决我的问题。
这是我的代码:
if ( is_user_logged_in() ) {
global $wpdb;
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$reservations = $wpdb->get_results($wpdb->prepare("SELECT * FROM
SaveContactForm7_1 WHERE user = %s", $username));
echo "<div align=\'center\'>";
echo "<table class=\'responsive-table\'>";
echo "<caption>Liste des réservations</caption>";
echo "<thead>";
echo "<tr>";
echo "<th scope=\'col\'>Nom</th>";
echo "<th scope=\'col\'>ID</th>";
echo "<th scope=\'col\'>Qualité</th>";
echo "<th scope=\'col\'>Arrivée</th>";
echo "<th scope=\'col\'>Num Vol</th>";
echo "<th scope=\'col\'>H. Att</th>";
echo "<th scope=\'col\'>Prov</th>";
echo "<th scope=\'col\'>Depart</th>";
echo "<th scope=\'col\'>Num Vol</th>";
echo "<th scope=\'col\'>H. Decl</th>";
echo "<th scope=\'col\'>Dest</th>";
echo "<th scope=\'col\'>Hôtel</th>";
echo "<th scope=\'col\'>Chambre</th>";
echo "<th scope=\'col\'>Total</th>";
echo "<th scope=\'col\'>Modifier</th>";
echo "<th scope=\'col\'>Effacer</th>";
echo "</tr>";
echo "</thead>";
foreach($reservations as $reservation){
echo "<tbody>";
echo "<tr>";
echo "<th scope=\'row\'>".$reservation->nom."</th>";
echo "<td data-title=\'ID\'>".$reservation->user."</td>";
echo "<td data-title=\'Qualité\'>".$reservation->qualite."</td>";
echo "<td data-title=\'Arrivée\'>".$reservation->datearrivee."</td>";
echo "<td data-title=\'N° Vol\'>".$reservation->num_vol_arrivee."</td>";
echo "<td data-title=\'Atterrissage\'>".$reservation->heure_atterrissage."
</td>";
echo "<td data-title=\'Provenance\'>".$reservation->provenance."</td>";
echo "<td data-title=\'Départ\'>".$reservation->datedepart."</td>";
echo "<td data-title=\'N° VOL\'>".$reservation->num_vol_depart."</td>";
echo "<td data-title=\'Décollage\'>".$reservation->heure_decollage."
</td>";
echo "<td data-title=\'Déstination\'>".$reservation->destination."
</td>";
echo "<td data-title=\'Choix\'>".$reservation->choix."</td>";
echo "<td data-title=\'Chambre\'>".$reservation->typech."</td>";
echo "<td data-title=\'Prix Total\' data-type=\'currency\'>".$reservation-
>calculated_choix."</td>";
这里是“删除”部分:
echo "<td align=\'center\'>
<***I need the code here "input" or "a href" ***><img class=\'icon\' width=\'16\' height=\'16\'
src=\'../remove-icon.png\' alt=\'Delete\' title=\'Delete\'></a>
</td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo "</div>";
}
最合适的回答,由SO网友:Prince J 整理而成
可以,您可以使用wpdb删除自定义表中的记录。wordpress函数中的类似内容。
require_once (\'../../../../wp-load.php\');
if (!empty($_POST[\'id\'])) {
global $wpdb;
$table=\'table_name\';
$id = $_POST[\'id\'];
$wpdb->delete( $table, array( \'id\' => $id ) );
}