【发布时间】:2017-11-16 06:02:57
【问题描述】:
我正在尝试在我的网站上实施一些 cookie。理想情况下,我想将用户 html 表单输入保存到 cookie 中,以便自动设置以供以后参考。然而,这种形式也会将值发送到数据库,如下所示:
<?php
session_start();
?>
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = false;
if(empty($_POST['formName']))
{
$errorMessage = true;
}
if(empty($_POST['formEmail']))
{
$errorMessage = true; }
if(empty($_POST['formAddress']))
{
$errorMessage = true; }
if(empty($_POST['formPrice']))
{
$errorMessage = true; }
$varName = $_POST['formName'];
$varEmail = $_POST['formEmail'];
$varAddress = $_POST['formAddress'];
$varPrice = $_POST['formPrice'];
$varComments = $_POST['formComments'];
if($errorMessage == false)
{
$db = mysql_connect("","","");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("" ,$db);
$sql = "INSERT INTO formdata (name, email, address, price, comments)
VALUES (".
PrepSQL($varName) . ", " .
PrepSQL($varEmail) . ", " .
PrepSQL($varAddress) . ", " .
PrepSQL($varPrice) . ", " .
PrepSQL($varComments) . ")";
mysql_query($sql);
header("Location: thankyou.php");
exit();
}
}
//sql injection protection..
function PrepSQL($value)
{
if(get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
?>
<!DOCTYPE html>
<html>
<body>
<div class="Formm">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php
if ( $errorMessage == true ) {
echo( "<h3>PLEASE FILL IN ALL QUESTIONS WITH A STAR.</h3>" );
}
$result = mysqli_query( $link, "SELECT * FROM `formdata` WHERE user=$user ORDER BY id ASC LIMIT 1" );
if ( mysqli_num_rows( $result ) > 0 ) {
// output data of each row
while ( $row = mysqli_fetch_assoc( $result ) ) {
$id = $row[ "id" ];
$varname = $row[ "varname" ];
$varemail = $row[ "varemail" ];
$varaddress = $row[ "varaddress" ];
$varprice = $row[ "varprice" ];
$varcomments = $row[ "varcomments" ];
}
}
?>
<p>
<label for='formName'>Venue name*</label><br/>
<input type="text" name="formName" maxlength="100" value="<? echo($varName); ?>"/>
</p>
<p>
<label for='formEmail'>Email*</label><br/>
<input type="text" name="formEmail" maxlength="100" value="<? echo($varEmail);?>"/>
</p>
<p>
<label for='formAddress'>Address*</label><br/>
<input type="text" name="formAddress" maxlength="100" value="<? echo($varAddress);?>"/>
</p>
<p>
<label for='formPrice'>Estimated price*</label><br/>
<input type="text" name="formPrice" maxlength="100" value="<?php echo($varPrice);?>"/>
</p>
<p>
<label for='formComments'>Any comments associated with the Venue that the artist should be aware of?</label><br/>
<input type="text" name="formComments" maxlength="250" value="<? echo($varComments);?>"/>
</p>
<input type="submit" name="formSubmit" value="Submit"/>
</form>
<?php
include 'footer.php';
?>
</body>
</html>
新编辑我正在尝试在我的网站上实施一些 cookie。理想情况下,我想将用户 html 表单输入保存到 cookie 中,以便自动设置以供以后参考。然而,这种形式也会将值发送到数据库,如下所示:
【问题讨论】:
-
这里没有足够的代码,你提到了一个数据库;问题不清楚。
-
@Fred-ii- 很抱歉,希望这次编辑能澄清一点
-
我注意到了编辑:任何地方都没有 cookie 代码。我们知道的越少,花费的时间就越多,记住这一点。我现在将不得不传递这个,让其他人看看你的帖子。
-
@Fred-ii- 明白了。只是我对如何实现这一点没有足够的了解。希望这个伪代码有点帮助。
-
我建议阅读 W3School's page 上关于 javascript 的 cookie。如果您仍然遇到问题,请使用不适合您的代码更新您的问题。您的问题目前没有显示您尝试设置 cookie 的代码或努力,因此它可能会出现“有人可以为我写我的 cookie 代码吗?”即使那不是你的意图。
标签: javascript php jquery html cookies