【问题标题】:The best way to insert an integer into my table using previous/next buttons使用上一个/下一个按钮将整数插入我的表的最佳方法
【发布时间】:2019-04-18 14:02:42
【问题描述】:

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

表格布局:

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

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();
?>

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

【问题讨论】:

  • 您需要保留用户历史记录还是只保留当前位置?如果您只想存储当前位置,请在页面加载时插入(或如果当前​​会话的记录存在则更新)一条记录。用户是否进入下一页或上一页无关紧要,您在幻灯片页面加载时更新数据库。
  • 你好@Vasya我只需要运行一个插入(或更新查询,如果一个整数已经保存在表中),当它们到达checkPoint的特定页面时,请说你可以帮忙哈哈!
  • 一个简单的方法是在 $(document).ready 事件中调用 php 脚本(使用 jquery)
  • 我可以将 SQL 查询包装在这个抱歉中吗?

标签: php html sql mysqli


【解决方案1】:

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

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

如果您不需要可靠的来源,在安全方面,您可以使用$_SERVER['SCRIPT_NAME']$_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 脚本中使用了。以及在按钮的 HTML 链接中使用的 $n 变量。

这是 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 扩展。

【讨论】:

    猜你喜欢
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2017-08-24
    • 2014-07-13
    相关资源
    最近更新 更多