0

I am trying to re-display information from a table onto a form with the information I get is where id= userid.

I think the only problem I am having is that don't understand how to use the result as some type of name to put the results into my form since I am using tokens(so the user can see what they put as their favorite food when they initially set up the account.
For example if it selects from a row of data that already exists where id= user_id how do I do something like this after the query below?

I am currently using:

$row = $stmt->fetchAll(); shown on bottom before the form This can't be correct so I think I need something like the code below but I don't understand what it's doing or even how to write it correctly (i found this somewhere and I think it's close?)

while($res = fetch_array($result)) { $name = $res['name']; $age = $res['favfood']; $email = $res['email']; }

// Everything below this point in the file is secured by the login system

    // We can retrieve a list of members from the database using a SELECT query. 
    // In this case we do not have a WHERE clause because we want to select all 
    // of the rows from the database table. 
     $query_params = array( 

            ':user_id' => $_SESSION['user']['id'], 
        ); 
    $query = " 
        SELECT 
            username, 
            favoritefood, 
            email 
        FROM order1new 
        WHERE id = :user_id
    "; 


    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Here I am trying to retrieve all of the information I need and put it       into the form below?
 fetchAll 
    $row = $stmt->fetchAll(); 
?> 

form name ="favfood" action="favfood.php" method="post"> 
    Username:<br /> 
    <input type="text" name="username" value="<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>" readonly>
    <br /><br /> 
    Food:<br /> 
    **<!--//figure out how to find these ECHO $ FOOD STUFFS!!!!-->
    <input type="number" name="favfood" value=<?php echo $favfood; ?> /> 
    <br /><br />** 
    Email:<br /> 
    <input type="email" name="email" value="<?php echo $email; ?>" /> 
    <br /><br /> 

    <input type="submit" value="Submit" /> 
</form>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Brady
  • 11
  • Since you are using `$row = $stmt->fetchAll();`, then instead of `echo $favfood`, you would do `echo $row[0]['favoritefood'];`. The `[0]`, because `->fetchAll()` returns an array of arrays, and you want the 1st array. The `favoritefood` as that is the name of your column in your query. – Sean Dec 19 '16 at 04:56
  • I don't want to use fetchall if possible. I want to fetch the item from the row containing the userid and display that favfood. I am having a disconnect where I run the query and want to fetch the information as something like $favfood. Does this make sense? – Brady Dec 19 '16 at 05:05
  • 1
    Mayhaps you are wanting `$stmt->fetch(PDO::FETCH_ASSOC);`? – Rasclatt Dec 19 '16 at 05:11
  • It seems like your asking how to access the values from the array returned by `fetchAll`. Try this http://stackoverflow.com/questions/10911757/how-to-use-pdo-to-fetch-results-array-in-php or this http://stackoverflow.com/questions/16846531/how-to-read-fetchpdofetch-assoc/16858666 –  Dec 19 '16 at 05:51
  • I guess I figured it out using fetchall – Brady Dec 19 '16 at 06:10
  • I ended up doing this in the form which I still don't know why – Brady Dec 19 '16 at 06:10

0 Answers0