【发布时间】:2014-09-25 18:05:36
【问题描述】:
我使用会话制作了一个用户登录-注销表单。我用于会话的代码是
retailer_login_session.php
<?php
$connection = mysqli_connect("as.com", "as", "as");
$db = mysqli_select_db("as", $connection);
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysqli_query("select * from retailer_signup where id='$user_check'", $connection);
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['id'];
$user_firstname = $row['firstname'];
$user_lastname = $row['lastname'];
if(!isset($login_session)){
mysqli_close($connection);
header('Location: index.html');
}
?>
例如能够为retailer_signup 是
id firstname lastname email password
1 f.retailer l.retailer retailer@gmail.com retailer
用户的主页需要显示来自table named retailer_add_property 的项目列表。连同列表一起,我希望在用户主页上显示零售商的 ID,并进一步将其保存到数据库中
例如,retailer_add_property 的表是
id propertyname propertytype retailerid
1 n.property t.property
我用来在用户个人资料页面上显示 id 的代码是
<div class="form-group">
<label class="col-lg-3 control-label">Retailer Unique ID:</label>
<? echo $login_session;?>
</div>
帮助在后端数据库中插入表单值的php代码是
<?php
include('retailer_login_session.php');
$con=mysqli_connect("ab.com","ab","ab","ab");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$propertyname = mysqli_real_escape_string($con, $_POST['propertyname']);
$propertytype = mysqli_real_escape_string($con, $_POST['propertytype']);
$sql="INSERT INTO retailer_add_property(propertyname,propertytype,retailerid) VALUES ('$propertyname','$propertytype','$login_session')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: index.html");
mysqli_close($con);
?>
我的问题是 id 的值既没有显示也没有存储在数据库中。希望能提供有关此问题的帮助
【问题讨论】:
-
在retailer_login_session.php 中,“session_start();”在第一行。
-
请don't use
mysql_*functions in new code。 它们不再被维护并且是officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi。 This article 将帮助您决定哪个。 -
@fortune 我已经在第三行的数据库连接后开始了会话
-
您的代码结构已关闭。如前所述,您正在混合 MySQL API。首先,您使用
retailer_login_session.php文件中的mysql_*函数进行连接,然后您将使用包含数据库连接的mysqli_*函数进行包含include('retailer_login_session.php');。您将需要使用单个数据库连接并使用mysqli_*函数独占。鉴于下面的答案,如果您尝试过但失败了,请问问自己为什么。 -
@Fred-ii- 干杯!。我什至没有注意到这一点。