我在下面有一个提取PHP脚本(useracc-test.php)。通过login.php登录后,我将进入用户页面(useracc-test.php)。在useracc-test.php中,我可以查看我的个人数据(id,name,username,telno)。但是当我按下提交按钮时,检索到的数据(id,name,username,telno)就会消失。因为在同一页面中,我也有一个插入表单。但首先,每次按下提交按钮时,如何保留检索到的数据(显示的个人数据)?请帮忙。我试过" print",但它只适用于我在插入表单中手动输入数据。我不能将它应用于检索到的数据(个人数据)。我下载了jQuery并了解了它,但仍然无法完全理解如何使用它。如果我可以使用PHP来保留数据,有没有办法?几天我试过互联网上的所有例子,我找不到或者找不到合适的解决方案。我失去了理智。请帮忙。
以下数据 - id,name,username,telno每次按提交时都会消失。如何保留它?
<?php
//useracc-test.php
//start session
session_start();
require 'connect-test.php';
if(isset($_POST['username'])){
$userName = $_POST['username'];
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
}
?>
<html>
<head>
</head>
<body>
<div id="apDiv3">
<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">Tab 1</li>
<li class="TabbedPanelsTab" tabindex="0">Tab 2</li>
</ul>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent">
<p>
</p>
<?php
echo $_SESSION['id']."<br/>";
echo $_SESSION['name']."<br/>";
echo $_SESSION['username']."<br/>";
echo $_SESSION['telno']."<br/>";
?>
<?php
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$name2 = $_POST['name2'];
$color2 = $_POST['color2'];
$hobby2 = $_POST['hobby2'];
$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2) VALUES (?,?,?,?)");
$stmt->bind_param("isss",$id,$name2,$color2,$hobby2);
$stmt->execute();
// $stmt->close();
// $conn->close();
}
?>
</p>
</div>
<div class="TabbedPanelsContent">
<form name="form2" method="post" action="useracc-test.php">
<p> </p>
<p>id :
<label for="id"></label>
<input name="id" type="text" id="id" value="<?php echo htmlspecialchars
($row['id']); ?>" >
</p>
<p>name :
<input type="text" name="name2" id="name2">
</p>
<p>color2 :
<input type="text" name="color2" id="color2">
</p>
<p>hobby2 :
<input type="text" name="hobby2" id="hobby2">
</p>
<p>
<input type="submit" name="submit" id="submit" value="submit">
</p>
</form>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
<script type="text/javascript">
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
</script>
<?php unset($_SESSION);
session_destroy(); ?>
</body>
</html>
答案 0 :(得分:1)
在fetch数组之后,您应该在$ _SESSION变量中分配结果
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
你的$ _SESSION变量将在每个有session_start()的页面中可用。
当用户注销时,您可以刷新$ _SESSION变量:
unset($_SESSION);
session_destroy();
要打印结果,您可以这样做:
<?php
echo $_SESSION['id']."<br/>";
echo $_SESSION['name']."<br/>";
echo $_SESSION['username']."<br/>";
echo $_SESSION['telno']."<br/>";
<?
编辑:像这样更改代码的顶部。每次点击''SUBMIT''时,查询都会再次执行,但由于此时$ _POST ['username']未设置,因此您在$ username变量中分配了一个空字符串
<?php
//useracc-test.php
//start session
session_start();
require 'connect-test.php';
if(isset($_POST['username'])){
$userName = $_POST['username'];
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
}
?>
答案 1 :(得分:0)
它不会保留,因为您的sql所需的用户名可能是从登录表单发布的。从useracc-test.php
刷新页面后,发现的数据无处可寻,因为它是从login.php检索的
现在您可以使用SESSIONS
session_start();
$_SESSION['username'] = $_POST['username'];
isset($_SESSION['username']) ? $id = $_SESSION['username'] : false;
$_SESSION
,就可以在任何地方使用 session_start()