A
A
Andrey Elsukov2020-05-14 16:29:12
PHP
Andrey Elsukov, 2020-05-14 16:29:12

Deleting and editing records in the database?

Hello!
The task is this:
I need to delete and edit an article in the database by pressing a button.
Can you tell me the best way to do this?
As I understand it at the moment, the "delete" and "edit" buttons need to pass the id of the article in which I clicked this button. How to do it??

If clumsily explained, sorry)

The base class code:

class base{

        public function connect(){
            return mysqli_connect('localhost', 'drops', '', 'teach');
        }
    
        public function get_data($query) {
            $connect = $this->connect();
            return mysqli_query($connect, $query);
        }

        public function print($var){
            echo "<pre>" . print_r($var, 1) . "</pre>";
        }
    }


Here is the code itself:
<?php
function post(){
$BD = new base();
$dat = $BD->get_data("SELECT * FROM song ORDER BY id");
$rows = mysqli_fetch_all($dat, MYSQLI_ASSOC);
return $rows;
}
$posts = post();
?>

<div class = 'bg'>
<div class = 'd-flex flex-wrap'>
        <div class='album'>
            <div class='container'>
                <div class='row'>
                <?php foreach($posts as $post): ?>
                    <div class='block'>
                        <p> id = <?php echo $post['id'] ?></p>
                        <div class='img'>
                            <?php echo $post['name']  ?>
                        </div>  
                        <div class='content-body'>
                            <p class='content-text'><?php echo $post['text']; ?></p>
                        </div>
                        <div class='but'>
                        <a href='./edition.php' <?php $post['id'] ?> class='butt'>Редактировать</a>
                        <a href='./delete.php' <?php $post['id'] ?> class='butto'>Удалить</a>
                        </div>
                    </div>
                    <?php endforeach ?>
                </div>
            </div>
        </div>
    <?php //endfor ?>
</div>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-14
@Dropsen

Well , at least so.

class base{
    public $conn;
    public function __construct(){
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        $this->conn = mysqli_connect('localhost', 'drops', '', 'teach');
        mysqli_set_charset($mysqli, $charset);
    }
    public function query($query, $params = []) {
        if (!$params) {
            return $this->conn->query($query);
        } 
        $types = $types ?: str_repeat("s", count($params));
        $stmt = $this->conn->prepare($sql);
        $stmt->bind_param($types, ...$params);
        $stmt->execute();
        return $stmt;
    }
    public function get_row($query, $params = []) {
        $stmt = $this->query($query, $params);
        return $stmt->get_result()->fetch_assoc(MYSQLI_ASSOC);
    }
    public function get_all($query, $params = []) {
        $stmt = $this->query($query, $params);
        return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    }
}

And how links are written in html, they will tell you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question