Home > front end >  How do I use data of database and insert it into a textfield on another php file
How do I use data of database and insert it into a textfield on another php file

Time:08-10

Iam currently trying to use my data in my database and insert it into a formular, which has multiple textfields. The formular is in a different php file and when I try to insert the data, nothing happens. Even if I have a blanked website and try to print it out via echo, none of the data inside my database is shown up.

The first file is where I get all my connections and and work with the database (config.php)

public function article_details($id){
    global $connection;
    $stmt = $connection->prepare("SELECT * FROM `items` WHERE item_name=:id");
    $stmt->bindparam(':id', $id);
    $stmt->execute();
 } 

this file is for the redirect when I click on the a tag and the magic happens (view.php)

<?php
$id=$_GET['id'];
include('config.php');

#header("Location: Website.php?page=add_article");
$call = new article();
$call->article_details($id);
foreach ($call as $row)
    echo $row['item_name']; ?>

Since its a bigger project with multiple files, I dont want to spamm them all in here. But if you need more information let me know.

CodePudding user response:

You need to return stmt object then call fetch function on it to display data

public function article_details($id){
    global $connection;
    $stmt = $connection->prepare("SELECT * FROM `items` WHERE item_name=:id");
    $stmt->bindparam(':id', $id);
    $stmt->execute();
     return $stmt;

 } 

$call = new article();
$stmt = $call->article_details($id);

while ($row = $stmt->fetch()) {
    echo $row['item_name']."<br />\n";
}

CodePudding user response:

Your article_details function is not returning anything so you are basically passing null to the foreach loop. I think this will help.

class article {

//...

    public function article_details($id){
        global $connection;
        $stmt = $connection->prepare("SELECT * FROM `items` WHERE item_name=:id");
        $stmt->bindparam(':id', $id);
        $stmt->execute();
       return $stmt;
    }
}

Then ...

$id=$_GET['id'];

include('config.php');


//header("Location: Website.php?page=add_article");


$myarticle = new article();

$call = $myarticle->article_details($id);

foreach ($call as $row){

    echo $row['item_name'];

}

Hope that helps.

  • Related