如何根据当前用户登录获取特定表

时间:2017-08-25 作者:Ramy Mâaouia

我正在使用联系人表格7和“保存联系人表格7”。我希望登录的用户只看到自己的字段。所以,只有当$reservation->user 等于$current_user->user_login.

顺便说一下,当用户提交联系人表单时,其用户值将自动生成为当前用户名。

[text* user readonly default:user_login]
所以,我的问题只是当我试图从数据库中获取表时。

我正在使用以下代码:

global $wpdb;

$current_user = wp_get_current_user();
$username     = $current_user->user_login;

echo \'Username: \' . $username . "\\n";

$reservations = $wpdb->get_results( "SELECT * FROM SaveContactForm7_1 WHERE user= \'***I need the condition here***\' ;" );

//print_r( $reservations );
对于将显示在我的页面中的表,以下是代码:

<?php
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Nom et Prenom</th>";
echo "<th>Qualite</th>";
echo "<th>Arrivee</th>";
echo "<th>Num Vol</th>";
echo "<th>Heure atterrissage</th>";
echo "<th>Provenance</th>";
echo "<th>Depart</th>";
echo "<th>Num Vol</th>";
echo "<th>Decollage</th>";
echo "<th>Destination</th>";
echo "<th>Hotel</th>";
echo "<th>Type de chambre</th>";
echo "<th>Total a payer</th>";
echo "</tr>";

foreach($reservations as $reservation){
echo "<tr>";
echo "<td>".$reservation->user."</td>";
echo "<td>".$reservation->nom."</td>";
echo "<td>".$reservation->qualite."</td>";
echo "<td>".$reservation->datearrivee."</td>";
echo "<td>".$reservation->num_vol_arrivee."</td>";
echo "<td>".$reservation->heure_atterrissage."</td>";
echo "<td>".$reservation->provenance."</td>";
echo "<td>".$reservation->datedepart."</td>";
echo "<td>".$reservation->num_vol_depart."</td>";
echo "<td>".$reservation->heure_decollage."</td>";
echo "<td>".$reservation->destination."</td>";
echo "<td>".$reservation->choix."</td>";
echo "<td>".$reservation->typech."</td>";
echo "<td>".$reservation->calculated_choix."</td>";
echo "</tr>";
}
echo "</table>";
?>

1 个回复
最合适的回答,由SO网友:ClemC 整理而成

Note 您可能需要使用WPDB::prepare() 防止SQL注入。虽然,在你的具体情况下,这可能是有争议的。。。

// If the query does not work, ensure the table name is correct...
$table = $wpdb->prefix . \'SaveContactForm7_1\';

$reservations = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM $table WHERE user = %s",
        $username
    )
);
请参见WPDB 类别参考。希望这能回答你的问题。

结束

相关推荐

WordPress 4.8.1使用的MySQL_CONNECT不适用于PHP7

我刚刚升级到PHP 7,却发现WordPress 4.8.1(最新版本)仍然在wp db中使用mysql\\u connect。php模块,但mysql\\u connect已被弃用。以下代码摘自wp db php,第1567-1571行:if ( WP_DEBUG ) { $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_f

如何根据当前用户登录获取特定表 - 小码农CODE - 行之有效找到问题解决它

如何根据当前用户登录获取特定表

时间:2017-08-25 作者:Ramy Mâaouia

我正在使用联系人表格7和“保存联系人表格7”。我希望登录的用户只看到自己的字段。所以,只有当$reservation->user 等于$current_user->user_login.

顺便说一下,当用户提交联系人表单时,其用户值将自动生成为当前用户名。

[text* user readonly default:user_login]
所以,我的问题只是当我试图从数据库中获取表时。

我正在使用以下代码:

global $wpdb;

$current_user = wp_get_current_user();
$username     = $current_user->user_login;

echo \'Username: \' . $username . "\\n";

$reservations = $wpdb->get_results( "SELECT * FROM SaveContactForm7_1 WHERE user= \'***I need the condition here***\' ;" );

//print_r( $reservations );
对于将显示在我的页面中的表,以下是代码:

<?php
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Nom et Prenom</th>";
echo "<th>Qualite</th>";
echo "<th>Arrivee</th>";
echo "<th>Num Vol</th>";
echo "<th>Heure atterrissage</th>";
echo "<th>Provenance</th>";
echo "<th>Depart</th>";
echo "<th>Num Vol</th>";
echo "<th>Decollage</th>";
echo "<th>Destination</th>";
echo "<th>Hotel</th>";
echo "<th>Type de chambre</th>";
echo "<th>Total a payer</th>";
echo "</tr>";

foreach($reservations as $reservation){
echo "<tr>";
echo "<td>".$reservation->user."</td>";
echo "<td>".$reservation->nom."</td>";
echo "<td>".$reservation->qualite."</td>";
echo "<td>".$reservation->datearrivee."</td>";
echo "<td>".$reservation->num_vol_arrivee."</td>";
echo "<td>".$reservation->heure_atterrissage."</td>";
echo "<td>".$reservation->provenance."</td>";
echo "<td>".$reservation->datedepart."</td>";
echo "<td>".$reservation->num_vol_depart."</td>";
echo "<td>".$reservation->heure_decollage."</td>";
echo "<td>".$reservation->destination."</td>";
echo "<td>".$reservation->choix."</td>";
echo "<td>".$reservation->typech."</td>";
echo "<td>".$reservation->calculated_choix."</td>";
echo "</tr>";
}
echo "</table>";
?>

1 个回复
最合适的回答,由SO网友:ClemC 整理而成

Note 您可能需要使用WPDB::prepare() 防止SQL注入。虽然,在你的具体情况下,这可能是有争议的。。。

// If the query does not work, ensure the table name is correct...
$table = $wpdb->prefix . \'SaveContactForm7_1\';

$reservations = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM $table WHERE user = %s",
        $username
    )
);
请参见WPDB 类别参考。希望这能回答你的问题。