1

I want to retrieve two values from my php function and print them into my form. Where exactly and how do i need to call my function: get_prof_set()

php code:

    if (!isset($_SESSION)) {

    session_start();
}

function get_prof_set() {

    $user_id = $_SESSION['user_id'];
    $query = "SELECT user_name, user_email FROM users WHERE user_id = '{$user_id}' ";

    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result)) {

        $user_name = $row['user_name'];
        $user_email = $row['user_email'];

    }
}

My form:

    require ('includes/functions/profile_func.php');

    <form action="./profile.php" method="post" id="form_profile">

                         <div>
                            <label>Name:</label>
                            <div>
                                <input id="user_name" type="text" name="user_name" value="<?php echo $user_name; ?> " />
                                <input id="hidden_user_name" type="hidden" name="hidden_user_name" value="<?php $user_name; ?>" />
                            </div>
                             <div >
                                <button type="submit" name="btnSave" id="btnSave">Save</button>
                            </div>
                            <div id="feedback_profile_name”>  </div>                              
                        </div>

                        <br>

                        <div>
                            <label>Email:</label>
                            <div>
                                <input id="user_email” type="text" name="user_email” value="<?php echo $user_email; ?> " />
                                <input id="hidden_user_email” type="hidden" name="hidden_user_email” value="<?php $user_email; ?>" />
                            </div>
                             <div >
                                <button type="submit" name="btnSave" id="btnSave">Save</button>
                            </div>
                            <div id="feedback_profile_email”>  </div>                              
                        </div>                        
</form>

2 Answers 2

2

Function (update return statement):

function get_prof_set() {

    $user_id = $_SESSION['user_id'];
    $query = "SELECT user_name, user_email FROM users WHERE user_id = '{$user_id}' ";

    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result)) {

        $user_name = $row['user_name'];
        $user_email = $row['user_email'];

    }
    /* UPDATE!!! */
    return array('name' => $user_name, 'email' => $user_email);
}

Form:

<?php
    require ('includes/functions/profile_func.php');
    $user = get_prof_set();
?>

<form action="./profile.php" method="post" id="form_profile">
    <div>
        <label>Name:</label>
        <div>
            <!-- Update!!! --><input id="user_name" type="text" name="user_name" value="<?php echo $user['name']; ?>" />
            <!-- Update!!! --><input id="hidden_user_name" type="hidden" name="hidden_user_name" value="<?php $user['name']; ?>" />
        </div>
        <div >
            <button type="submit" name="btnSave" id="btnSave">Save</button>
        </div>
        <div id="feedback_profile_name”>  </div>                              
    </div>
    <br>
    <div>
        <label>Email:</label>
        <div>
            <!-- Update!!! --><input id="user_email” type="text" name="user_email” value="<?php echo $user['email']; ?>" />
            <!-- Update!!! --><input id="hidden_user_email” type="hidden" name="hidden_user_email” value="<?php $user['email']; ?>" />
        </div>
        <div >
            <button type="submit" name="btnSave" id="btnSave">Save</button>
        </div>
        <div id="feedback_profile_email”>  </div>                              
    </div>                        
</form>

Hope You get the idea, function return array. Then You can use this array later :).

BTW: You should make a statement with LIMIT 1 query (I think that there is only one user with a given id). Just for safety.

BTW2: It is better to use this function once (as it connects to a database), remember the user in the variable, and then only echo it. Another answer is also correct, but it says that You should use this function few times which is not correct.

As You are not using classes, and this is a basic example thats all. Remember to switch to PDO in the future: http://php.net/manual/en/book.pdo.php.

http://php.net/manual/en/function.mysql-query.php - notice the red box ;).

Sign up to request clarification or add additional context in comments.

2 Comments

OK no problem, I hope my answer is useful ;).
Thx for accepted in that case. And remember about PDO, as mysql function will be removed in the nearest future. Have a nice day :).
1

You can call this function anywhere after require statement and before using its variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.