Php 如何在数据库中存储$\u会话值
我一直在尝试存储会话Php 如何在数据库中存储$\u会话值,php,mysql,mysqli,Php,Mysql,Mysqli,我一直在尝试存储会话userUid,并将其放在id下的数据库中。我希望它合并来自不同数据库的两个id 我尝试将userUid设置为变量并将其放入数据库userUid是注册/登录页面的id,profiles是我试图将其插入的数据库 if(!isset($_SESSION['userUid'])){ header("location: index.php"); // Redirecting To Home Page $switch = $_SESSION['userUid'];
userUid
,并将其放在id下的数据库中。我希望它合并来自不同数据库的两个id
我尝试将userUid
设置为变量并将其放入数据库userUid
是注册/登录页面的id,profiles是我试图将其插入的数据库
if(!isset($_SESSION['userUid'])){
header("location: index.php"); // Redirecting To Home Page
$switch = $_SESSION['userUid'];
$Asql = "INSERT INTO profile (id) VALUES ('$switch');" ;
mysqli_query($conn2, $Asql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['id'] . "<br>";
}
}
}
if(!isset($\u会话['userUid']){
标题(“location:index.php”);//重定向到主页
$switch=$\会话['userUid'];
$Asql=“插入配置文件(id)值(“$switch”);”;
mysqli_查询($conn2,$Asql);
$resultCheck=mysqli_num_行($result);
如果($resultCheck>0){
while($row=mysqli\u fetch\u assoc($result)){
echo$row['id']。“
”;
}
}
}
我希望它将新插入的
$\u会话['userUid']
作为“id”从数据库配置文件中输出。您试图在插入查询中使用mysqli\u fetch\u assoc
。您可以只使用
$\u SESSION['userUid']
,也可以进行单独的select查询以从数据库中获取新行。首先检查代码中的“if”条件。
if(!isset($\u会话['userUid']):
...
其他:
...
endif;
你的最终解决方案
if(!isset($_SESSION['userUid'])):
header("location: index.php"); // Redirecting To Home Page
else:
$switch = $_SESSION['userUid'];
$Asql = "INSERT INTO profile (id) VALUES ('$switch');" ;
mysqli_query($conn2, $Asql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0):
while ($row = mysqli_fetch_assoc($result))
{
echo $row['id'] . "<br>";
}
endif;
endif;
if(!isset($\u会话['userUid']):
标题(“location:index.php”);//重定向到主页
其他:
$switch=$\会话['userUid'];
$Asql=“插入配置文件(id)值(“$switch”);”;
mysqli_查询($conn2,$Asql);
$resultCheck=mysqli_num_行($result);
如果($resultCheck>0):
while($row=mysqli\u fetch\u assoc($result))
{
echo$row['id']。“
”;
}
endif;
endif;
需要注意的几件事
- 您当前的逻辑永远不会真正起作用,因为您只在未设置会话值时执行查询
- 你应该总是
退出代码>在
标题(“位置:…”)之后代码>呼叫
- 您应该使用准备好的语句,并通过占位符绑定您的值
- 插入查询没有与之关联的
(插入/更新/删除查询的等效方法是num_行
)或受影响的_行
方法获取*()
if(!isset($\u会话['userUid']){
标题(“location:index.php”);//重定向到主页
出口
}
$sql=“插入配置文件(id)值(?);”;
$stmt=$conn2->prepare($sql);
$stmt->bind_param(“s”,会话['userUid');
$stmt->execute();
echo$stmt->受影响的行。“插入行”;
$stmt->close();
为了正确地检查错误,您应该在创建MySQLi连接之前通过添加以下行来启用MySQLi异常模式。这意味着您必须对查询语句使用try/catch
,但反过来又意味着您不必对每个函数调用执行单独的错误检查
mysqli_报告(mysqli_报告错误| mysqli_报告严格);