简体   繁体   English

如何获取表中特定记录的ID - PHP

[英]How to get id of specific record in a table - PHP

I am trying to change the value of the is_public column in the MySQL article table using a toggle switch.我正在尝试使用切换开关更改 MySQL 文章表中 is_public 列的值。 if I wrap the input(the toggle) inside to get record's id using $_GET method for making necessary changes in the articles.php then it does not fetch data of is_public into the toggle.如果我使用 $_GET 方法将输入(切换)包装在里面以获取记录的 ID,以便在文章中进行必要的更改,那么它不会将 is_public 的数据提取到切换中。 is there any other way to get the id of a specific record?有没有其他方法可以获取特定记录的ID?

screenshot of the article list table文章列表截图

this is all-articles.php code:这是 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>

Well, You can send the article id in your form and then listen to it and do what you want with it.好吧,您可以在表单中发送文章 ID,然后收听它并使用它做您想做的事情。 So,所以,

// 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> 

Then in your /all-articles.php check for the $_POST['check'] which should give article id .然后在您的/all-articles.php检查$_POST['check']应该给出article id query your database with that id and toggle the is_public column使用该 ID 查询您的数据库并切换is_public

Another way would be to send a hidden field, some thing like另一种方法是发送一个隐藏字段,比如

// 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM