使用 php 将会话电子邮件插入 MySQL table
Inserting Session Email into MySQL table using php
MySQL table 'users' 结构:
id | user_name | user_pass | user_email |
1 | anderson | password |boy@mail.com|
MySQL table 'history' 结构:
id | user_email |
1 | boy@mail.com | <<< trying to insert boy@mail.com under user_email
试图将 boy@mail 插入到 user_email
下的 'history' table
嗨,我在 MySQL 数据库中创建了两个 table。第一个称为用户,第二个称为历史记录。我有一个登录和注册 PHP 代码,允许用户使用 'users' table 注册和登录。当我登录时,该页面将我重定向到一个欢迎页面,其中使用 $_SESSION 显示我的 user_email,如下所示。
现在我想做以下事情;如果用户已登录,则将 $_SESSION['email'] 插入 user_email.
下的 'history' table
我得到了这段代码,但我知道它真的错了。但我是 php 的新手。所以有人可以帮我修复它,并将第一个 table 的会话数据插入第二个 table,只有当用户登录时。非常感谢!
============================================= =========================
MY-CODE:用于尝试将会话值插入 'history' table
<head>
<?php
session_start();
$_SESSION['email'];
$query = "insert into history (email) values ('$email')";
?>
</head>
由于 OP 提供了非常模糊的细节,可以建议以下解决方案,这是非常容易实现的,而且麻烦很少
正如问题中提到的 OP "I have a login and registration PHP code that allows users to register and login using the 'users' table. When I login, the page redirects me to a welcome page"
在您的登录页面上,您可以在其中检查用户名和密码,并在验证后将用户重定向到欢迎页面,在重定向之前和验证之后 运行 此插入查询
$query = "INSERT INTO history (user_email) VALUES ('$email')";
$result = mysql_query($query);
在同一页上。不要忘记定义 $email
变量 First
不需要 $_SESSION['email'];
无需编写新查询
无需额外编码
更新;
OP 在评论中提供了代码
if(isset($_POST['login'])){
$password = mysql_real_escape_string($_POST['pass']);
$email = mysql_real_escape_string($_POST['email']);
$check_user = "SELECT * FROM users WHERE user_pass='$password' AND user_email='$email'";
$run = mysql_query($check_user);
$row= mysql_fetch_array($check_user);
$email = $row['user_email'];
if(mysql_num_rows($run)>0){
$_SESSION['email']=$email; //If you don't need this, remove this
$query = "INSERT INTO history (user_email) values ('$email')";
$result = mysql_query($query);
echo "<script>window.open('welcome.php','_self')</script>";
} else {
echo "<script>alert('Email or password x ')</script>";
}
}
MySQL table 'users' 结构:
id | user_name | user_pass | user_email |
1 | anderson | password |boy@mail.com|
MySQL table 'history' 结构:
id | user_email |
1 | boy@mail.com | <<< trying to insert boy@mail.com under user_email
试图将 boy@mail 插入到 user_email
下的 'history' table嗨,我在 MySQL 数据库中创建了两个 table。第一个称为用户,第二个称为历史记录。我有一个登录和注册 PHP 代码,允许用户使用 'users' table 注册和登录。当我登录时,该页面将我重定向到一个欢迎页面,其中使用 $_SESSION 显示我的 user_email,如下所示。
现在我想做以下事情;如果用户已登录,则将 $_SESSION['email'] 插入 user_email.
下的 'history' table我得到了这段代码,但我知道它真的错了。但我是 php 的新手。所以有人可以帮我修复它,并将第一个 table 的会话数据插入第二个 table,只有当用户登录时。非常感谢!
============================================= ========================= MY-CODE:用于尝试将会话值插入 'history' table
<head>
<?php
session_start();
$_SESSION['email'];
$query = "insert into history (email) values ('$email')";
?>
</head>
由于 OP 提供了非常模糊的细节,可以建议以下解决方案,这是非常容易实现的,而且麻烦很少
正如问题中提到的 OP "I have a login and registration PHP code that allows users to register and login using the 'users' table. When I login, the page redirects me to a welcome page"
在您的登录页面上,您可以在其中检查用户名和密码,并在验证后将用户重定向到欢迎页面,在重定向之前和验证之后 运行 此插入查询
$query = "INSERT INTO history (user_email) VALUES ('$email')";
$result = mysql_query($query);
在同一页上。不要忘记定义 $email
变量 First
不需要 $_SESSION['email'];
无需编写新查询
无需额外编码
更新;
OP 在评论中提供了代码
if(isset($_POST['login'])){
$password = mysql_real_escape_string($_POST['pass']);
$email = mysql_real_escape_string($_POST['email']);
$check_user = "SELECT * FROM users WHERE user_pass='$password' AND user_email='$email'";
$run = mysql_query($check_user);
$row= mysql_fetch_array($check_user);
$email = $row['user_email'];
if(mysql_num_rows($run)>0){
$_SESSION['email']=$email; //If you don't need this, remove this
$query = "INSERT INTO history (user_email) values ('$email')";
$result = mysql_query($query);
echo "<script>window.open('welcome.php','_self')</script>";
} else {
echo "<script>alert('Email or password x ')</script>";
}
}