【问题标题】:How to get id of specific record in a table - PHP如何获取表中特定记录的ID - PHP
【发布时间】:2020-01-31 21:03:07
【问题描述】:

我正在尝试使用切换开关更改 MySQL 文章表中 is_public 列的值。 如果我将输入(切换)包装在内部以使用 $_GET 方法在articles.php中进行必要的更改以获取记录的ID,那么它不会将is_public的数据获取到切换中。还有其他方法可以获取特定记录的 id 吗?

screenshot of the article list table

这是 all-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>

【问题讨论】:

  • 你需要更清楚一点,因为你想做什么??您是否希望能够单击某篇文章并能够在其他页面中对其进行编辑??\
  • @alithedeveloper 我想更改 MySQL 文章表中 is_public 列中的值,我的意思是当切换打开时,is_public 中的值更改为 1,如果它被关闭值应该更改为 0。为此,我需要获取该特定行/记录的 id。
  • 文章表中是否包含primary key(例如id)?
  • @showdev 是的。

标签: php mysql toggle


【解决方案1】:

好吧,您可以在表单中发送文章 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多