在php中创建一个多步骤表单

时间:2014-10-07 11:53:19

标签: php mysql sql mysqli

我正在尝试以多个步骤创建表单。我想要做的是在第一页用户将输入两个字段,即标题和描述将保存在数据库中,然后他们被重定向到第二页,他们需要更多的细节,但在这里我想放条件是这些值应该添加到具有上一页保存的title值的行的前面。

这是我到目前为止所做的事情

<?php
include('creator_session.php');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
     //necessary data
  </head>
  <body>
  <form class="form-horizontal" role="form" action="insert_page_one.php" enctype="multipart/form-data" method="post">
    <div class="row">
        <div class="col-md-4 col-md-offset-4"><input class="form-control" value="Project Name" type="text" name="title"></div>
    </div>

    <div class="row">
        <div class="col-md-4 col-md-offset-4"><input class="form-control" value="Project Name" type="text" name="title"></div>
    </div>

    <div class="row">
        <div class="col-md-4 col-md-offset-4"><input class="btn btn-primary" value="Get Started" type="submit" name="submit"></div>
    </div>
</form>
<body>
</html>

insert_page_one.php

<?php
include('creator_session.php');
$con=mysqli_connect("xyz.com","xyz","xyz","xyz");
if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
// escape variables for security
$title = mysqli_real_escape_string($con, $_POST['title']);
$category = mysqli_real_escape_string($con, $_POST['category']);

$sql="INSERT INTO creatorproject(title,category) VALUES ('$title','$category')";

if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }

header("Location: page_two.php");
mysqli_close($con);
?>

page_two.php

<?php
include('creator_session.php');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
     //necessary data
  </head>
<body>
<form class="form-horizontal" role="form" action="creator_page_two.php" enctype="multipart/form-data" method="post">
    <div class="form-group">
        <label class="col-md-3 control-label">Description</label>
            <div class="col-md-8">
                <input class="form-control" name="description" value="" type="">
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Project Location:</label>
            <div class="col-md-8">
                <input class="form-control" name="projectlocation" value="" type="">
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Funding Goal:</label>
            <div class="col-md-8">
                <input class="form-control" name="funding" value="" type="">
            </div>
    </div>              

    <div class="submit">
        <input type="submit" value="SAVE">
    </div>
</form>
</body>
</html>

creator_page_two.php

<?php
include('creator_session.php');

$con=mysqli_connect("xyz","xyz","xyz","xyz");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security

$description = mysqli_real_escape_string($con, $_POST['description']);
$projectlocation = mysqli_real_escape_string($con, $_POST['projectlocation']);
$funding = mysqli_real_escape_string($con, $_POST['funding']);

$sql = "UPDATE creatorproject SET description='".$description."', projectlocation='".$projectlocation."', funding='".$funding."'  WHERE title ='".$title."'";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
header("Location: creator_add_project.php");
exit;

mysqli_close($con);
?>

当我运行此代码时,它不会从第二页插入值..会感谢一些帮助,如果有人可以解释该过程,我会很感激,因为我是这个领域的新手,并且很想了解它。

P.S-我还使用了creator_session来存储用户的登录详细信息,因为用户必须在登录后填写这些表单,因此整个登录过程已经完成了会话

1 个答案:

答案 0 :(得分:1)

funding='".$funding."', WHERE应该与 creator_page_two.php

中的funding='".$funding."' WHERE类似

删除WHERE之前的逗号(,)。

修改

insert_page_one.php 中将header("Location: page_two.php");更改为header("Location: page_two.php?title=".urlencode($title));;

page_two.php 更改

<form class="form-horizontal" role="form" action="creator_page_two.php" enctype="multipart/form-data" method="post">

<form class="form-horizontal" role="form" action="creator_page_two.php?title=<?php echo $_GET['title'] ?>" enctype="multipart/form-data" method="post">

creator_page_two.php 中添加$title= urldecode($_GET['title']);