我正在尝试分多个步骤创建表单。我要做的是在第一页中,用户将输入两个字段,即标题和描述,它们将保存在数据库中,然后他们被重定向到需要更多详细信息的第二页,但我想在这里输入条件是这些值应该添加到前一页中保存的具有 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 来存储用户的登录详细信息,因为用户必须在登录后填写这些表格,因此整个登录过程已经通过 session 完成
最佳答案
funding='".$funding."', WHERE
应该看起来像 funding='".$funding."' WHERE
在 creator_page_two.php
删除 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']);
关于php - 在php中创建一个多步骤表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26235378/