Connection to db SQLITE and SELECT * FROM reciclagem_data WHERE $variable

Asked

Viewed 861 times

-1

I am making a program that allows the user to enter the ID of the person in question and later shows in a table all the data of it.

So far, I have these two codes:

<!doctype html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Untitled Document</title>
        <link rel="stylesheet" href="/css/main.css">
    </head>

    <body>
        <form action="search_php.php" method="post">
            ID: <input type="text" name="name"><br>
            <input type="submit">
        </form>

    </body>

    </html>

And

<?php
$db = "database_arduino.db"
$name = ["name"]

$sql = "SELECT * FROM reciclagem_data WHERE personID = $name"

?>

    <!doctype html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Untitled Document</title>
        <link rel="stylesheet" href="/css/main.css">
    </head>

    <body>
        <table></table>

    </body>

    </html>

From what I know, I’m having problems with the connection of PHP to the SQLITE3 database and, in the second code, on the line SELECT * FROM reciclagem_data WHERE $name. I want the program to record the variable that the user enters and then, with the same database, it is used to display the user’s data.

2 answers

1


You are seeking the value of $_POST incorrectly here:

$name = ["name"]

An example of how to pull it:

$name = filter_input(INPUT_POST, 'name');

Using the filter_input, also helps against SQL Injection.


A form of connection/query with Sqlite:

$obj = new SQLite3;
$obj -> open("local_do_arquivo_db");
$obj -> query("sua_query");

About using PDO, it should be analyzed. It would be good to research more about, as sometimes you will use something without need.

  • HTML, which is yours form. The form will send the data to your file with the PHP script. It will process and display the content. This

  • In view of this you have much simpler doubts than you want to do. You need to know at least how html works and php colleague. No use running over things. Try to see the basics before, if it will not be too difficult something that is simple. I suggest a free course EXTREMELY good: Cursoemvid

  • 1

    Even so, man, you’re starting at the end. It’s gonna be complicated. Look for xampp (serivdor php), after that you’ll have to release sqlite driver (php.ini) ... after that, you’ll need to know what you’re doing in html/php, if it won’t be too much trouble. Good luck.

1

To connect to SQLITE you must use PDO

<?php 
     // Cria uma conexão com o banco de dados indicado no caminho
     $myPDO = new PDO('sqlite:/home/example/books.db'); 

     // Para rodar uma query
     $result = $myPDO->query("SELECT * FROM reciclagem_data WHERE $name");
?>

An example to fill the table after picking the result:

<table>
    <thead>
        <th>Coluna 1</th>
        <th>Coluna 2</th>
        <th>Coluna 3</th>
    </thead>
    <?php foreach($result as $row) { ?>
        <tbody>
            <td><?php echo $row['nome'] ?></td>
            <td><?php echo $row['idade'] ?></td>
            <td><?php echo $row['sexo'] ?></td>
        </tbody>
    <?php } ?>
</table>
  • Thanks for the answer. The problem is that an error appears. Cannot POST. It’s from $name. What I do?

  • 2

    you must use => you can use

Browser other questions tagged

You are not signed in. Login or sign up in order to post.