如何获取 table - PHP 中特定记录的 ID
How to get id of specific record in a table - PHP
我正在尝试使用切换开关更改 MySQL 文章 table 中 is_public 列的值。
如果我将输入(切换)包装在里面以使用 $_GET 方法在 articles.php 中进行必要的更改来获取记录的 ID,那么它不会将 is_public 的数据提取到切换中。还有其他方法可以获取特定记录的 ID 吗?
screenshot of the article list table
这是全部-articles.php代码:
<table id="datatable" class="table table-hover" style="width:100%">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Author</th>
<th>Abstract</th>
<th>category</th>
<th>Date</th>
<th>Action</th>
<th>Publish</th>
</tr>
</thead>
<tbody>
<?php foreach($articles as $key => $article): ?>
<tr>
<td><?php echo $key+1 ?></td>
<td><?php echo $article['title'] ?></td>
<td><?php echo $article['author_id'] ?></td>
<td><?php echo $article['abstract'] ?></td>
<td><?php echo $article['category_id'] ?></td>
<td><?php echo $article['entry_date'] ?></td>
<td>
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-primary btn-sm">Edit</button>
<button type="button" class="btn btn-success btn-sm">Show</button>
<button type="button" class="btn btn-danger btn-sm">Delete</button>
</div>
</td>
<td>
<form action="all-articles.php" method="post">
<label class="custom-toggle">
<?php if($article['is_public']): ?>
<input type="checkbox" checked name="check">
<?php else: ?>
<input type="checkbox" name="uncheck">
<?php endif; ?>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
好吧,您可以在表单中发送文章 ID,然后收听它并用它做您想做的事。所以,
// As you are already in foreach loop so,
<form action="all-articles.php" method="post">
<label class="custom-toggle">
<?php if($article['is_public']): ?>
<input type="checkbox" checked name="published" value="<?php echo $article['id']; ?>>
<?php else: ?>
<input type="checkbox" name="published" value="<?php echo $article['id']; ?>>
<?php endif; ?>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</form>
然后在您的 /all-articles.php
中检查 $_POST['check']
应该给出 article id
。使用该 ID 查询您的数据库并切换 is_public
列
另一种方法是发送一个隐藏字段,比如
// As you are already in foreach loop so,
<form action="all-articles.php" method="post">
<label class="custom-toggle">
<?php if($article['is_public']): ?>
<input type="checkbox" checked name="published" value="<?php echo $article['id']; ?>>
<input type="hidden" name="is_published" value="yes"> or 1
<?php else: ?>
<input type="hidden" name="is_published" value="no"> or 0
<input type="checkbox" name="published" value="<?php echo $article['id']; ?>>
<?php endif; ?>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</form>
我正在尝试使用切换开关更改 MySQL 文章 table 中 is_public 列的值。 如果我将输入(切换)包装在里面以使用 $_GET 方法在 articles.php 中进行必要的更改来获取记录的 ID,那么它不会将 is_public 的数据提取到切换中。还有其他方法可以获取特定记录的 ID 吗?
screenshot of the article list table
这是全部-articles.php代码:
<table id="datatable" class="table table-hover" style="width:100%">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Author</th>
<th>Abstract</th>
<th>category</th>
<th>Date</th>
<th>Action</th>
<th>Publish</th>
</tr>
</thead>
<tbody>
<?php foreach($articles as $key => $article): ?>
<tr>
<td><?php echo $key+1 ?></td>
<td><?php echo $article['title'] ?></td>
<td><?php echo $article['author_id'] ?></td>
<td><?php echo $article['abstract'] ?></td>
<td><?php echo $article['category_id'] ?></td>
<td><?php echo $article['entry_date'] ?></td>
<td>
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-primary btn-sm">Edit</button>
<button type="button" class="btn btn-success btn-sm">Show</button>
<button type="button" class="btn btn-danger btn-sm">Delete</button>
</div>
</td>
<td>
<form action="all-articles.php" method="post">
<label class="custom-toggle">
<?php if($article['is_public']): ?>
<input type="checkbox" checked name="check">
<?php else: ?>
<input type="checkbox" name="uncheck">
<?php endif; ?>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
好吧,您可以在表单中发送文章 ID,然后收听它并用它做您想做的事。所以,
// As you are already in foreach loop so,
<form action="all-articles.php" method="post">
<label class="custom-toggle">
<?php if($article['is_public']): ?>
<input type="checkbox" checked name="published" value="<?php echo $article['id']; ?>>
<?php else: ?>
<input type="checkbox" name="published" value="<?php echo $article['id']; ?>>
<?php endif; ?>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</form>
然后在您的 /all-articles.php
中检查 $_POST['check']
应该给出 article id
。使用该 ID 查询您的数据库并切换 is_public
列
另一种方法是发送一个隐藏字段,比如
// As you are already in foreach loop so,
<form action="all-articles.php" method="post">
<label class="custom-toggle">
<?php if($article['is_public']): ?>
<input type="checkbox" checked name="published" value="<?php echo $article['id']; ?>>
<input type="hidden" name="is_published" value="yes"> or 1
<?php else: ?>
<input type="hidden" name="is_published" value="no"> or 0
<input type="checkbox" name="published" value="<?php echo $article['id']; ?>>
<?php endif; ?>
<span class="custom-toggle-slider rounded-circle"></span>
</label>
</form>