简体   繁体   中英

How to echo mysql results to a text field

this script displays data from a specific email address which the user enters.

the snippet of code below displays the data in a textfield at the top of the page however I want to display the data in a textfield in the body of text.

echo '<input name="login" type="text" value="' . $result['name'] . '>';

What do I change in the above code to enable me to do this.

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="orders"; // Table name  

$email = $_POST['textfield'];


        $db = new PDO('mysql:host='.$host.
                          ';dbname='.$db_name.
                          ';charset=UTF-8',
                    $username, $password);
        $stmt = $db->prepare('SELECT * FROM `orders` WHERE `email`=:email LIMIT 1');
        $stmt->bindValue(':email', $email, PDO::PARAM_STR);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if($stmt->rowCount()>0)
{
echo '<input name="login" type="text" value="' . $result['name'] . '>';
}
else
{
    echo "Email not found in the database!";
}


  ?>

form:

<form id="form_53" name="login" action="test.php">

   <input type="submit" value="Track">
   <input type="text" username="textfield" value="">
   <input type="text" name="name" value="<?php echo $result['name']?>"> //I want to display results here

</form>

if both the code exists in the same file.. this will work

<input type="text" name="name" value="<?php echo $result['name']?>">

and if you want to check if there are rows returned you can use

<input type="text" name="name" value="<?php echo ($stmt->rowCount()>0) ? $result['name'] : "" ?>">

edited the code so that you know what exactly you want to do

First: Replace this code

if($stmt->rowCount()>0)
{
    echo '<input name="login" type="text" value="' . $result['name'] . '>';
}
else
{
    echo "Email not found in the database!";
}

just keep $result = $stmt->fetch(PDO::FETCH_ASSOC); and

 if($stmt->rowCount()<=0) echo "Email not found in the database!";

Second: now in HTML section

<input type="text" name="name" value="<?php echo ($stmt->rowCount()>0) ? $result['name'] : "" ?>">

只要你不破坏$result你就可以:

<input type="text" name="name" value="<?php echo $result['name']; ?>"> //I want to display results here
echo '<input name="login" type="text" value="' . $result['name'] . '">';

只需在echo字符串中添加双引号即可。

It's pretty easy to accomplish. Of course make sure that the $results array is included in the session for the desired HTML page that you are working on.

<form id="form_53" name="login" action="test.php">

  <input type="submit" value="Track">
  <input type="text" username="textfield" value="">
  <input type="text" name="name" value="<?php echo $results['name']?>"> //I want to display results here

</form>

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