我实际上是PHP新手。我的问题是会话不存储在我的数据库中。
这是我的demo_session1.php
<?php
$Refno="";
// Start the session
session_start();
$_SESSION["$Refno"] = "HNo1";
echo "Ref no.:HNO1";
if(isset($_POST['submit']))
{
$Refno = $_POST['Refno'];
}
?>
<html>
<body>
<a href="demo_session2.php">Arrange to view property</a>
</body>
</html>
这是我的demo_session2.php:
<?php
$Refno = $message= "";
$messageErr = "";
session_start();
require "configdemo.php";
echo "Ref no.: " . $_SESSION["$Refno"] . ".<br>";
if(isset($_POST['submit']))
{
$Refno = $_POST['Refno'];
$message = $_POST['message'];
}
if (empty($_POST["message"]))
{
$messageErr = "Please leave a message for the agent.";
}
else
{
$sql = mysqli_query($con,"INSERT INTO demo (Refno, message) VALUES
('$Refno' , '$message') ") or die("Can't Insert! ");
header ("location: thanks.php");
}
?>
<style>
.error {
color: #FF0000;
font-size:12px;
text-transform: lowercase;
}
</style>
<!DOCTYPE html>
<html>
<body>
<table width="100%" align="center" cellpadding="0" cellspacing="1" >
<form name="viewing_form" method="post" action="">
<tr>
<td>Message: <td><textarea name="message"rows="5" cols="25" value="<?php echo
$message;?>"></textarea>
<td><span class="error"> <?php echo $messageErr;?></span></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
<td><input type="submit" name="cancel" value="Cancel"></td>
</body>
</html>
“Refno”显示在demo_session2.php上,但是当我按下提交按钮时,只会将消息插入到数据库中。我也希望插入“Refno”。
答案 0 :(得分:2)
如果我看到的正确,您的表单不包含名称为&#34; Refno&#34;的字段。所以没有$ _POST [&#39; Refno&#39;]。因此,要么添加这样的字段,要么在QUERY中使用SESSION:
$ sql = mysqli_query($ con,&#34; INSERT INTO demo(Refno,message)VALUES (&#39; $ _ SESSION [Refno]&#39;,&#39; $ message&#39;)&#34;)或死亡(&#34;无法插入!&#34;);
正如卡雷尔所说,用mysqli_real_escape_string()
来逃避$消息是合适的