【问题标题】:Storing data on multiple pages with PHP使用 PHP 在多个页面上存储数据
【发布时间】:2016-10-01 20:39:30
【问题描述】:

我正在构建基于博客模型的业务规划器。但是,我不能在多个页面上执行此操作。在第 1 页上,当用户单击“下一步”时,就像单击“发布”一样。数据被存储并添加到其他业务计划或“帖子”列表中。在第一页中,数据是通过“插入”到数据库中来存储的。我想也许我可以简单地“更新”第 2 页上的数据库等等,以消除多个列出的“帖子”或商业计划。数据是从第一页存储的,而不是从第二页存储的。如果我使用“INSERT INTO ...”为每个页面添加数据,但每个页面都是作为单独的业务计划或多个帖子收集的。任何建议表示赞赏。

这是第 1 页:

  <?php
    session_start();
    include_once("db.php");    

if(isset($_POST['post'])) {
    $title = strip_tags($_POST['title']);
    $compName = strip_tags($_POST['compName']);
    $createdBy = strip_tags($_POST['createdBy']);
    $phone = strip_tags($_POST['phone']);

    $title = mysqli_real_escape_string($db,$title);
    $compName = mysqli_real_escape_string($db,$compName);
    $createdBy = mysqli_real_escape_string($db,$createdBy);
    $phone = mysqli_real_escape_string($db,$phone); 

    $date = date('l jS \of F Y h:i A');

    $sql = "INSERT INTO plans (title, date, compName, createdBy, phone) VALUES('$title', '$date', '$compName', '$createdBy', '$phone')";

    mysqli_query($db, $sql);
    header("Location: post2.php");
    }
?>

<!DOCTYPE html>
<html lang="en">
<div class="container">
<head>
  <title>Create Business Plan</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
    <h2>Create a new business plan</h2>
    <form action="post1.php" method="post" enctype="multipart/form-data">
        <br /><br />
        <p>Plan Title:  <input name="title" type="text" autofocus size="48"></p>
        <p>Company Name:  <input name="compName" type="text" autofocus size="48"></p>
        <p>Business Type: <input placeholder="Ie. Inc. (USA) or Oy (Finland)" name="bizType" type="text" autofocus size="48"></p>           
        <p>Created By:  <input name="createdBy" type="text" autofocus size="48"></p>
        <p>Phone:  <input name="phone" type="text" autofocus size="48"></p>
        <br /><br />

        <form action="<?php 'post2.php=?pid=$id'; ?>">
        <input name="post" type="submit" value="Next">
        </form>
        <br /><br />
    </form>

</body>
</div>
</html>

这是第 2 页:

   <?php
    session_start();
    include_once("db.php");

    if(isset($_POST['post'])) {
        $marketPlan = strip_tags($_POST['marketPlan']);
        $economics = strip_tags($_POST['economics']);
        $products = strip_tags($_POST['products']);
        $customers = strip_tags($_POST['customers']);

        $marketPlan = mysqli_real_escape_string($db,$marketPlan);
        $economics = mysqli_real_escape_string($db,$economics);
        $products = mysqli_real_escape_string($db,$products);
        $customers = mysqli_real_escape_string($db,$customers);

        $date = date('l jS \of F Y h:i A');

        $sql = "UPDATE plans SET marketPlan='$marketPlan', economics='$economics', products='$products', customers='$customers' WHERE id=$pid";

        mysqli_query($db, $sql);
        header("Location: post3.php"); //CHANGE LOCATION FOR NEXT PAGE
    }
    ?>

    <!DOCTYPE html>
    <html lang="en">
    <div class="container">
    <head>
      <title>Create Business Plan</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>
    <body>
        <h2>The Marketing Plan</h2>
        <form action="post2.php" method="post" enctype="multipart/form-data"> 
            <br /><br />            
        <h4>Market Research</h4>
        <p>The marketing plan requires intensive research for the type of industry your business wants to enter. It is very dangerous to assume that you are already well informed about your intended market. Market research is vital to make sure you are up to date. Use the business planning process as your opportunity to uncover data and to question your marketing efforts.</p>
  <textarea name="marketPlan" rows="15" cols="100"></textarea></p><hr />

<h4>Economics</h4>
                <p>What is the total size of your market? What percent of market share will you have? (This is important only if you think you will be a major factor in the market.) What is currently in demand in your target market? What are the trends in target market—growth, consumer preferences, and in product development? Growth potential and opportunity for a business of your size. 
<textarea name="economics" rows="15" cols="100"></textarea><hr />

<h4>Products</h4>
<p>In the <i>Products and Services</i> section, you described your products and services from your point of view. Now describe them from how your customers see them.</p><br /><textarea name="products" rows="15" cols="100"></textarea></p><hr />

<h4>Customers</h4>
<p>Identify your targeted customers, their characteristics, and their geographical location. This is known as customer demographics.</p>
<textarea name="customers" rows="15" cols="100"></textarea></p>

<input name="post" type="submit" value="Next">
</form>

    </body>
    </div>
    </html>

【问题讨论】:

    标签: php html mysql session


    【解决方案1】:

    您必须将新创建记录的ID传递到下一页;最安全的方法可能是将其存储在会话中。所以,在第一页,改变这个:

    mysqli_query($db, $sql);
    

    $query = mysqli_query($db, $sql);
    $_SESSION['new_plan_id'] = $query->insert_id;
    

    然后,在您的第二页再次查询 ID

    if (isset($_SESSION['new_plan_id']))
    {
      $pid = $_SESSION['new_plan_id'];
    }
    else
    {
      die('No valid session');
    }
    

    在用户完成他或她的计划后,从会话中删除 new_plan_id。您可能还想将 session_start() 添加到全局包含中,以便始终启用它。

    【讨论】:

      【解决方案2】:

      做一些这样的事情,而不是将数据插入和更新到数据库,将值存储在会话变量中,我的意思是

      例子:

      $_SESSION["title"] = strip_tags($_POST['title']);
      

      像这样将每个选项存储在会话变量中,直到到达最后一页。

      终于在最后一页了

      将其插入数据库。像这样,

      insert into table ('title',.....) values ($_SESSION["title"],....);
      

      当您想要跨多个页面传递数据时,请始终使用会话。

      【讨论】:

      • 谢谢@Charles_R!给你一个快速的问题。对于 $_SESSION["title"] = strip_tags($_POST['title']);我注意到我收到很多错误,说“未定义的索引”,然后提到该行。我玩过它,我已经完成了这两个:$_SESSION['$title'] = strip_tags($_POST['title']);,标题前面有$,这个:$_SESSION['title'] = strip_tags($_POST['title']); 没有美元符号。如果美元符号存在,它会影响 strip_tags 函数的性能吗?与 mysqli_real_escape_string 相同。提前致谢!
      • @MangoPie 抱歉,伙计,实际上我对性能变化了解不多。但我想它不会有太大变化。
      【解决方案3】:

      在第二页你必须从你的 url 中获取 id

      if (isset($_GET['pid']))
         $pid = $_GET['pid'];
      

      当您尝试更新未定义的 id 时

      【讨论】:

      • sigh 通过 URL 发布 ID 非常不安全,应该禁止。您认为所有这些数据泄露是如何发生的?
      【解决方案4】:

      我认为你只是做对了。第一篇文章必须在表格中插入这些详细信息,下一篇文章更新您要更新的字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-31
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多