php - 单击按钮时在另一个页面上显示值,在PHP中,值存储在数据库中

标签 php html mysql

我正在 PHP 的管理页面中发布公告部分。我想通过点击按钮直接传递公告到我网站的主页。我正在使用 phpmyadmin mysql。

这是管理屏幕- announcement make

<?php
            if (isset($_POST['submit_make'])) {
            $sql = "SELECT * FROM announce_table";
            $result = $con->query($sql);
            }
            ?>
<input type="submit" class="btn btn-info" name="submit_make" value="Make"/>

这是主页公告部分,我想从按钮获取数据-- homepage-where-to-print

<h4>fresh news</h4>
      <p>
    <!--/here i want to get that announcement when button click/-->
       </p>

最佳答案

根据您的信息:您有一个公告部分来创建公告,然后您在管理部分中检索所有公告。 单击“发布”公告后,您想在主页中查看该公告吗?如果是这样的话:

在 sql 的公告表中创建一个“status”列。例如:

ID | titel | message | status 

enter image description here

So when you are populating your announcements, create a form
around the "make" button, like this:

<?php foreach($announcements as $announcement){ ?>

 <div>
 <h4><?php echo $announcement.title; ?></h4>
 <p><?php echo $announcement.message; ?></p>
 <form type="post" action="make_announcement.php">
 <input type="hidden" name="id" value="<?php echo $announcement.id; ?>" />
 <button type="submit" name="make_announcement">Make</button>
 </form>
 <form type="post" action="delete_announcement.php">
 <input type="hidden" name="id" value="<?php echo $announcement.id; ?>" />
 <button type="submit" name="delete_announcement">Make</button> 
 <!-- this is submit to make_announcement.php -->
 </form>

 </div>     ?>

然后在你的 make_announcement.php 中

<?php

if(isset($_POST['make_announcement')){

$id = $_POST['id'];
$sql = "UPDATE announcement_table SET status = 1 WHERE id=$id;"; 

//then run the query and update the status.

最后在您的主页中获取状态 == 1 的所有公告。

关于php - 单击按钮时在另一个页面上显示值,在PHP中,值存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48756864/

相关文章:

php - MYSQL 查询在 PHP 中使用 if else 更新或插入

html - 我的边框底边被切断

php - foreach 循环插入所有复选框(选中和未选中)

mysql - 从 mysql 列中删除所有数字字符

php - MySQL Date_format错误

php - Swiftmailer:无法连接 TLS 加密

php - 了解PHP严格-我为什么要关心?

html - 如何获得悬停时显示的 div,而不是将内容向下推到页面上?

javascript - 如何为移动用户 (Android/iPhone) 的 "Add to home screen"响应式网站创建链接

mysql - 如何访问包含点的列名。在选择查询中