简体   繁体   中英

Delete entry from page but keep in DB (PHP)

Say I had the following:

$stmt = $pdo->prepare("SELECT jobName, jobDesc FROM Jobs");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $jName = $row['jobName'];
    $jDesc = $row['jobDesc'];
    print '<span class="job">Headline:</span> ' . $jName . '<br />
           <span class="job">Description:</span>
           <p class="job">' . $jDesc . '</p>';
} 

The HTML output would be (for testing purposes)

<span class="job">Headline:</span> test 1
<span class="job>Description:</span>
<p class="job">test 1 Description</p>

<span class="job">Headline:</span> test 2
<span class="job>Description:</span>
<p class="job">test 2 Description</p>

<span class="job">Headline:</span> test 3
<span class="job>Description:</span>
<p class="job">test 3 Description</p>

If I wanted to remove one of these entries from the page, but keep it in the DB, how would I accomplish this with PHP? I'd probably have to put a link or button on each entry to remove it, and I know I can't actually take it off the page permanently with jQuery unless I use something to save the DOM state.

Thank you

我建议沿着SHOULD_DISPLAY的行向数据库中的行添加一个标志,然后为不想显示的行设置标志,并在该查询中将其过滤掉(或者由于某种原因在显示时将其过滤掉)更有意义)。

There are different ways to accomplish this.

You could create a RESTful controller in PHP to implement basic CRUD operations, and then use jquery to delete/update each html element from your DOM and your DB.

Or you could just add a flag field to your database table, for example called "deleted", that can be true or false and let your php script render the row if it was not previously marked as deleted.

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