简体   繁体   中英

Get username with javascript to the value of the clause where in sql select

I don't think that I have to use javascript here. But if this is the solution, I will take it.

I am able to echo the SESSION of Username in my pages. But when I try to call it again in order to get the detail of the user information, it doesn't work.

Here is the scenario when it doesn't work:

  • I echo the session of username in the clause of sql select with the code bellow:

     $member_email = $_SESSION['member_email']; $member_name = $_SESSION['member_name']; $results = $mysqli->query("SELECT * FROM member_registry WHERE member_name='$member_name' And member_email='$member_email'"); $obj = $results->fetch_object(); echo '<tr><td colspan="6"><input type="text" readonly name="member_address" value="'.$obj->member_address_satu.'"/></td><tr>'; 

It works when I input the value of username directly in the clause of where of sql select

    $results = $mysqli->query("SELECT * FROM member_registry WHERE member_name='Someone Name'");

I am very sure that the Session of username is fine. Becuase when I echo it, it shows the username:

        <li>You Login as <?php echo $member_name;?></li>

If I can't use clause of WHERE member_name='$member_name' then I think I can use javascript to send the text from You login as <?php echo $member_name;? as the string to the clause.

How to do this, and how to solve this problem cleanly. Please help, I am just learning and learning and learning.

I hope anyone understand what I am asking here. Many thanks for all of that.

[UPDATED]

<?php
include_once("config.php");
$member_name = $_SESSION['member_name'];
$results = $mysqli->prepare("SELECT * FROM member_registry WHERE member_name=?");
$bind=$mysqli->bind_param('s',$member_name);
$mysqli->execute(); 
$obj = $results->fetch_object();

if(isset($_SESSION["products"])) {
    $total = 0;
    echo '<form action="cart-post-config.php" enctype="multipart/form-data" method="POST">';
    foreach ($_SESSION["products"] as $cart_itm) {
                    echo '<tr>';
                    echo '<td></td>';
                    echo '<input type="hidden" name="member_name" value="'.$member_name.'"/>';
                    echo '<td><input type="text" readonly name="product_name[]" value="'.$cart_itm["name"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_price[]" value="'.$cart_itm["price"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_quantity[]" value="'.$cart_itm["qty"].'"/></td>';                           
                    $totalPerEach = ($cart_itm["price"]*$cart_itm["qty"]);

                    echo '<td><input type="text" readonly name="product_eachTotal[]" value="'.$totalPerEach.'"/></td>';
                    echo '<td>View Save Delete</td>';
                    echo '</tr>';
    }


    echo '<tr><td colspan="6"><input type="text" readonly name="member_address" value="'.$obj->member_address_satu.'"/></td><tr>';                          

    echo '<tr><td colspan="6" style="text-align: right; background: yellowgreen;"><input type="submit" name="submit" /></td><tr>';
    echo '</form>';//close the form
}
else {
    echo 'Your Cart is empty'; }
?>

This is at the header of the page.

<?php
include("config.php");  
$member_name = $_SESSION['member_name'];
$member_email = $_SESSION['member_email'];
?>
    <ul id="list-logo-menu">
        <li>Live Chat</li>
        <li>Contact</li>
        <li>You Login as <?php echo $member_name;?></li>
        <li><a href="logout.php">Logout</a></li>
    </ul>

[UPDATE 2]

                    <tr class="boder-table">
                        <td width="32"><u>No</u></td>
                        <td width="174"><u>Product Name</u></td>
                        <td width="103"><u>Price</u></td>
                        <td width="103"><u>Quantity</u></td>
                        <td width="103"><u>Total Price</u></td>
                        <td width="142"><u>Mark</u></td>
                    </tr>



 $member_name = $_SESSION['member_name'];
 $results = $mysqli->prepare("SELECT * FROM member_registry WHERE member_name=? ");
 $bind=$mysqli->bind_param('s',$member_name);//bind them here

 $result= $mysqli->execute();//execute your query
 print_r($result).'<br/>';


 if($result->num_rows<0)
 {
 $mysqli->error;
 } else echo '<br/>'.$mysqli->num_rows;


if(isset($_SESSION["products"])) {
    include_once("config.php");
    $total = 0;
    echo '<form action="cart-post-config.php" enctype="multipart/form-data" method="POST">';
    foreach ($_SESSION["products"] as $cart_itm) {
                    echo '<tr>';
                    echo '<td></td>';
                    echo '<input type="hidden" name="member_name" value="'.$member_name.'"/>';
                    echo '<td><input type="text" readonly name="product_name[]" value="'.$cart_itm["name"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_price[]" value="'.$cart_itm["price"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_quantity[]" value="'.$cart_itm["qty"].'"/></td>';                           
                    $totalPerEach = ($cart_itm["price"]*$cart_itm["qty"]);

                    echo '<td><input type="text" readonly name="product_eachTotal[]" value="'.$totalPerEach.'"/></td>';
                    echo '<td>View Save Delete</td>';
                    echo '</tr>';
    }


    echo '<tr><td colspan="6"><input type="text" readonly name="member_address" value="'.$obj->member_address_satu.'"/></td><tr>';                          

    echo '<tr><td colspan="6" style="text-align: right; background: yellowgreen;"><input type="submit" name="submit" /></td><tr>';
    echo '</form>';//close the form
}
else {
    echo 'Your Cart is empty'; }
?>
            </table>

I just move the location where I should write the new sql statements.

<?php


if(isset($_SESSION["products"])) {
    include_once("config.php");
    $total = 0;
    echo '<form action="cart-post-config.php" enctype="multipart/form-data" method="POST">';
    foreach ($_SESSION["products"] as $cart_itm) {
                    echo '<tr>';
                    echo '<td></td>';
                    echo '<input type="hidden" name="member_name" value="'.$member_name.'"/>';
                    echo '<td><input type="text" readonly name="product_name[]" value="'.$cart_itm["name"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_price[]" value="'.$cart_itm["price"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_quantity[]" value="'.$cart_itm["qty"].'"/></td>';                           
                    $totalPerEach = ($cart_itm["price"]*$cart_itm["qty"]);

                    echo '<td><input type="text" readonly name="product_eachTotal[]" value="'.$totalPerEach.'"/></td>';
                    echo '<td>View Save Delete</td>';
                    echo '</tr>';
    }

    $member_name = $_SESSION['member_name'];
    $results = $mysqli->query("SELECT * FROM member_registry WHERE member_name='$member_name'");
    $obj = $results->fetch_object();
    echo '<span class="product-name">'.$obj->member_address_satu.'</span></br>';

    echo '<tr><td colspan="6"><input type="text" readonly name="member_address" value="'.$obj->member_address_satu.'"/></td><tr>';                          

    echo '<tr><td colspan="6" style="text-align: right; background: yellowgreen;"><input type="submit" name="submit" /></td><tr>';
    echo '</form>';//close the form
}
else {
    echo 'Your Cart is empty'; }

?>

You need to use the bind_param

        $member_name = $_SESSION['member_name'];
$results = $mysqli->prepare("SELECT * FROM member_registry WHERE member_name=?");
    $bind=$mysqli->bind_param('s',$member_name);//bind them here

$result= $mysqli->execute();//execute your query
$row = $result->fetch_array(MYSQLI_ASSOC);
print_r($row).'<br/>'; // to see the result. 


if($result->num_rows<0)
{
    $mysqli->error;//to check the error
} else echo '<br/>'.$mysqli->num_rows;//to print the row.


if(isset($_SESSION["products"])) {
    include_once("config.php");
    $total = 0;
    echo '<form action="cart-post-config.php" enctype="multipart/form-data" method="POST">';
    foreach ($_SESSION["products"] as $cart_itm) {
                    echo '<tr>';
                    echo '<td></td>';
                    echo '<input type="hidden" name="member_name" value="'.$member_name.'"/>';
                    echo '<td><input type="text" readonly name="product_name[]" value="'.$cart_itm["name"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_price[]" value="'.$cart_itm["price"].'"/></td>';
                    echo '<td><input type="text" readonly name="product_quantity[]" value="'.$cart_itm["qty"].'"/></td>';                           
                    $totalPerEach = ($cart_itm["price"]*$cart_itm["qty"]);

                    echo '<td><input type="text" readonly name="product_eachTotal[]" value="'.$totalPerEach.'"/></td>';
                    echo '<td>View Save Delete</td>';
                    echo '</tr>';
    }


    echo '<tr><td colspan="6"><input type="text" readonly name="member_address" value="'.$obj->member_address_satu.'"/></td><tr>';                          

    echo '<tr><td colspan="6" style="text-align: right; background: yellowgreen;"><input type="submit" name="submit" /></td><tr>';
    echo '</form>';//close the form
}
else {
    echo 'Your Cart is empty'; }
?>
            </table>

@klaudia,First check your mysqli connection to database is proper or not. The sample connection for mysqli as below.

$mysqli = mysqli_connect("localhost","mysql_user","mysql_password","database");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Then Check your session details proper or not via print session.

echo $_SESSION['member_email'];
echo $_SESSION['member_name'];

Then you need to fire below query as per your requirement.

//Below is sample membername & email,you can put your session data in below two line.

$member_email = 'user@user.com';
$member_name = 'test';

//For Your scenario it would be like below. (Rememeber you need to put session_start() in top of the page)

$member_email = $_SESSION['member_email'];
$member_name = $_SESSION['member_name'];

$results = $mysqli->query("SELECT * FROM member_registry WHERE member_name='$member_name' And member_email='$member_email'");
$obj = $results->fetch_object();
echo "<pre>";print_r($obj);

Please try above & let me know if still any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM