在我的数据库php pdo查询中显示文本字段中的数据

时间:2014-08-20 06:51:03

标签: php html pdo textbox

我已经建立了与数据库的连接我也能够进行搜索查询然后我偶然发现了这个问题。如何在textfield / inputfield中显示/显示这些数据。

我已阅读过thisthis等来源,但不幸的是他们没有提供答案,并且感谢任何建议。

更新 我希望显示显示来自php查询的数据,如果可能的话,我的意思是在查询后我会显示数据,如果有可用的话,如果没有什么都不会显示

    if(!empty($_POST['upcomingeventwhat'])){
            $upcomingeventwhat = trim($_POST['upcomingeventwhat']);

            $stmt = $dbh->prepare("SELECT * FROM events WHERE event_what = ? ") ;
            $stmt->bindValue(1,$upcomingeventwhat);
            $stmt->execute();
            $selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
            //$data = $stmt->fetch();
            if($selected_row){
                echo $selected_row['event_what'];
                echo $selected_row['event_where'];
                echo $selected_row['event_when'];
                echo $selected_row['event_who'];
       ***

//make this echo show in the textfield in html
    //example i have this text field in html
    //<input  type="text" name="upcomingeventwho" maxlength="50"

*** 
            }else{
                echo "No event found";
            }
        }else{
            echo "what is empty";    
        }

1 个答案:

答案 0 :(得分:0)

好的,我的问题很简单吗

echo '<input  type="text" name="upcomingeventwhat" maxlength="50" value="'.$selected_row['event_what'].'"> ';

echo '<input  type="text" name="upcomingeventwhere" maxlength="50" value="'.$selected_row['event_where'].'"> ';

echo '<input  type="text" name="upcomingeventwhen" maxlength="50" value="'.$selected_row['event_when'].'"> ';

echo '<input  type="text" name="upcomingeventwho" maxlength="50" value="'.$selected_row['event_who'].'"> ';

更新2:

$event_what = "";
$event_where ="";
$event_when ="";
$event_who ="";
// set above variable blank first

if(!empty($_POST['upcomingeventwhat'])){
            $upcomingeventwhat = trim($_POST['upcomingeventwhat']);

            $stmt = $dbh->prepare("SELECT * FROM events WHERE event_what = ? ") ;
            $stmt->bindValue(1,$upcomingeventwhat);
            $stmt->execute();
            $selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
            //$data = $stmt->fetch();
            if($selected_row){
                $event_what = $selected_row['event_what'];
                $event_where = $selected_row['event_where'];
                $event_when = $selected_row['event_when'];
                $event_who = $selected_row['event_who'];

                 // assign them database value  

            }else{
                echo "No event found";
            }
        }else{
            echo "what is empty";    
        }

// now you can use that variables in textfield value like below

echo '<input  type="text" name="upcomingeventwhat" maxlength="50" value="'.$event_what.'"> ';

    echo '<input  type="text" name="upcomingeventwhere" maxlength="50" value="'.$event_where.'"> ';

    echo '<input  type="text" name="upcomingeventwhen" maxlength="50" value="'.$event_when.'"> ';

    echo '<input  type="text" name="upcomingeventwho" maxlength="50" value="'.$event_who.'"> ';