表单数据插入数据库后如何清除
Posted
技术标签:
【中文标题】表单数据插入数据库后如何清除【英文标题】:How do I clear the form data after it has been inserted into database 【发布时间】:2016-03-11 02:17:08 【问题描述】:数据提交并插入数据库后,如何清除表单数据?因此,用户不能在提交数据后按返回和刷新来重新输入数据,因为目前他们可以。另外请忽略mysql root登录,仅供测试使用。
这是我的代码
<?php
session_start();
if (isset($_POST['sub']))
$host='localhost';
$user='root';
$pass='';
$db='theatre_booking';
$con=mysqli_connect($host,$user,$pass,$db);
$row = $_POST['row'];
$_SESSION["row"]=$row;
$zone = $_POST['zone'];
$_SESSION["zone"]=$zone;
$quantity = $_POST['qty'];
$_SESSION["qty"]=$quantity;
$quantity = $_POST['quotation'];
$_SESSION["quotation"]=$quantity;
$sql="INSERT INTO booking(PerfDate, PerfTime, Name, Email, RowNumber)
VALUES (
'$_SESSION['date']',
'$_SESSION['time']',
'$_SESSION['name']',
'$_SESSION['email']',
'$_SESSION['row']')";
if ($con->query($sql) === TRUE)
echo "Booking successful";
else
echo "Error: " . $sql . "<br>" . $con->error;
?>
<!------------------------------------------------------------------------------------>
<DOCTYPE! html>
<html>
<head>
<meta charset="uft-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"">
<meta name= "viewport" content="width=device-width, initial-scale=1.0">
<link href="style/styles.css" type="text/css" rel="stylesheet">
</head>
<body>
<h2 id="siteTitle">Westend</h2>
<nav>
<ul class="main_menu">
<li><a href="index.php">Home</a></li>
<li><a href="booking.php" >Booking</a></li>
</ul>
</nav>
<!--Image shown in the background after booking is confirmed-->
<div><img src="images/curtains.jpg" id="open"/>
<!--Booking confirmation message the user sees-->
<div id="process">
</br>Hi <?php echo "'".$_SESSION['name']."'"; ?></br>
Your tickets have been booked for <?php echo "'".$_SESSION['production']."'";?>
</br>Playing on <?php echo "'".$_SESSION['date']."'";?> at <?php echo "'".$_SESSION['time']."'";?> </br>
You are in <?php echo $_POST["zone"];?> in row <?php echo $_POST["row"];?></br>
The total cost is £<?php echo $_POST["quotation"];?></br>
Confirmation of your booking has been sent to: <?php echo "'".$_SESSION['email']."'"; ?></br>
</br>
Enjoy the show!
</br>
</div>
</div>
<!--Footer-->
<footer>
<p class="pageBottom">© Westend 2015</p>
</footer>
</body>
</html>
【问题讨论】:
为什么将这些值放在 $_SESSION 中?使用简单的变量,您将解决问题。 P.S 我建议你在 $_POST 变量中做一些控制以避免sql注入攻击 嗨@Alessandro.Vegna $_Session 保存着从表单前几页收集的值,因此被结转了。以及如何向 $_POST 变量添加控件? 对于控件只需看看这里***.com/questions/60174/…。好的,所以你可以在 $sql = .... 另一个建议改为使用 使用 = $_SESSION["name"] ?> 它更具可读性。我认为你必须在你的 php.ini 中激活 short_open_tags 感谢@Alessandro.Vegna 【参考方案1】:我会在这里回答,所以我可以添加一些代码
if(isset($_POST["row"] && !empty($_POST["row"])
$sql="INSERT INTO booking(PerfDate, PerfTime, Name, Email, RowNumber)
VALUES (
'$_SESSION['date']',
'$_SESSION['time']',
'$_SESSION['name']',
'$_SESSION['email']',
'$_SESSION['row']')";
if ($con->query($sql) === TRUE)
echo "Booking successful";
else
echo "Error: " . $sql . "<br>" . $con->error;
【讨论】:
以上是关于表单数据插入数据库后如何清除的主要内容,如果未能解决你的问题,请参考以下文章