0

I'm using contact form 7 and "save contact form 7". I want that the logged user see only his own fields. So, He'll see results only if $reservation->user is equal to $current_user->user_login.

By the way, when the user submit the contact form, his user value is automatically generated as the current username.

[text* user readonly default:user_login]

So, my problem is only when I try to get the table from my database.

I'm using the following code:

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 );

For the table that will appear in my page, this is the code:

<?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>";
?>
  • Is this a contact form 7 question? That database table is non-standard/3rd party, how will we know what its contents/format are? User could be a user ID, or a username, edit your question and let us know – Tom J Nowell Aug 25 '17 at 12:45
  • I edited my question. Yes, I'm using both "contact form 7" and "save contact form 7". Also, I set the user field as default readonly current "user_login" to get automatically the current user_login value. Hope It's clear now. Thanks. – Ramy Mâaouia Aug 25 '17 at 12:54
0

Note that you may need to use WPDB::prepare() to prevent against SQL injections. Although, it might be arguable in your specific case...

// 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
    )
);

See WPDB class reference. Hope this answers to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.