首页 > 解决方案 > 使用上一个/下一个按钮将整数插入我的表的最佳方法

问题描述

我有一个幻灯片,用户可以在其中单击一个带有箭头的按钮,指向下一个和上一个。当用户单击箭头时,我想在其中保存页面名称,以便将它们重定向到正确的页面。我还想存储一个数字,这样当用户单击上一个或下一个时,整数将保存在正确的表中userTakingModule,位于checkPoint.

表布局: 桌子

执行此操作的最佳方法是什么,即在button标签内使用form标签?当用户单击两个箭头之一时,我已经粘贴了迄今为止尝试实现的两种方式:

a.) 通过将 page1.html 在action一个箭头中,将 page3.html 在另一个箭头中,使用户返回一页。 b.) 在按钮单击时将值保存到userTakingModule表中。

html 尝试 1.)

<div class="arrow-container">
  <form style="display: inline" action="dashboard.php" method="post">
  <button value="1"><i class="fas fa-arrow-circle-left"></i></button>
  </form>

<form style="display: inline" action="slide2.php" method="post">
  <button value="3"><i class="fas fa-arrow-circle-right"></i></button>
</form>
</div>

html 尝试 2.)

<div>
<form action ="dashboard.php" method = "POST"> <!-- However I may need this page link to change depending on whether they click forward or back -->
    <label>Competition Categories</label>
    <select name="checkPoint">
    <option value="">Select</option>
    <option value="1">Previous</option>
    <option value="3">Next</option>
    </select>
    </fieldset>
    <button name="save_check_point" type="submit" type="button">View</button>
 </form>
</div>

到目前为止我的查询是这样的:

<?php

  // The module ID will always be 5, as they are in the Stress & Anxiety slideshow which has an ID of 5 in the table.
  $idUsers = $_SESSION['id'];
  $ModuleID = 5;
  $checkPoint = $_POST['checkPoint'];

        // Preparing and binding my SQL query which inserts a checkpoint number into the table
        $stmt = $conn->prepare ("INSERT INTO `userTakingModule` (`userTakingModuleID`, `idUsers`, `ModuleID`, `checkPoint`) VALUES (NULL, ?, ?, ?)");

        $stmt->bind_param("iii", $idUsers, $ModuleID, $checkPoint);
        $stmt->execute();
?>

我真的在这方面苦苦挣扎了一段时间,所以在这方面的任何帮助都会很棒,提前谢谢你。

标签: phphtmlsqlmysqli

解决方案


我会建议一个更简单的方法。如果您的页面具有相同的名称格式:'page'后跟页码,并以文件扩展名结尾(例如 page2.html);那么您不需要使用表单,因为 PHP 可以为您提供:

  • 当前页面的地址(包括脚本名称)
  • 将用户代理引向当前页面的页面地址(如果有)。

如果您不需要可靠的来源,在安全方面,您可以使用$_SERVER['SCRIPT_NAME']and$_SERVER['HTTP_REFERER']获取信息并使用简单的锚点来链接页面。

这是必须插入所有幻灯片页面中的 PHP 代码,在您的 PHP 代码(看起来不错)之前和 HTML 按钮之前:

<?php

//A simple function to parse script names and get your page number
function page_number($script_name){
  //Remove the first '/' character and the word 'page' to get '#.php'
  $number = substr($script_name,1+strlen('page'));
  //Remove the extension part to get just the page number 
  $number = substr($number,0,strpos($number,'.')); //
  return $number;
}

//Get the page script name, like '/page#.php'
$script_name = $_SERVER['SCRIPT_NAME'];
$n = page_number($script_name);

//Get the referer URL
$referer = $_SERVER['HTTP_REFERER'];
//Get the script name of that URL
$referer = substr($referer,strrpos($referer,'/'));
$checkPoint = page_number($referer);

?>

当此 PHP 代码结束时,您就$checkPoint可以在 PHP 脚本中使用整数了。$n以及在按钮的 HTML 链接中使用的变量。

这是prevnext按钮的 HTML:

<div class="arrow-container">
  <a href="page<?php echo($n-1); ?>.php">
    <i class="fas fa-arrow-circle-left"></i>
  </a>

  <a href="page<?php echo($n+1); ?>.php">
    <i class="fas fa-arrow-circle-right"></i>
  </a>
</div>

PS:请注意页面需要.php扩展名而不是.html扩展名才能工作。


推荐阅读