【发布时间】:2016-07-10 03:25:42
【问题描述】:
我目前正在创建一个基于足球的应用程序。最初,用户需要登录才能使用存储在数据库中的应用程序。在此之后,用户被呈现到主菜单,可以在其中选择一个部分来对地面进行评级。从这里,评级也可以保存并存储到数据库中。我目前拥有的数据库包含 2 个表,其中一个以 userid 作为主键:
tbl_client CREATE TABLE `tbl_client` (
`userid` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(35) NOT NULL,
`email` varchar(25) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
评分表的主键为 id,外键为 userid。
tbl_rating CREATE TABLE `tbl_rating` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`userid` int(3) NOT NULL,
`arsenal` varchar(11) NOT NULL,
`chelsea` varchar(11) NOT NULL,
`liverpool` varchar(11) NOT NULL,
`manchesterC` varchar(11) NOT NULL,
`manchesterU` varchar(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
CONSTRAINT `tbl_rating_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `tbl_client` (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
对于我的评级 php 文件,我有:
<?php
error_reporting(0);
include_once 'init.php';
$arsenal = $_POST["arsenal"];
$chelsea = $_POST["chelsea"];
$liverpool = $_POST["liverpool"];
$manchesterC = $_POST["manchesterC"];
$manchesterU= $_POST["manchesterU"];
$sql = "INSERT INTO `tbl_rating` (`id`,`userid`,`arsenal`, `chelsea`, `liverpool`,`manchesterC`, `manchesterU`) VALUES (NULL, NULL,'".$arsenal."', '".$chelsea."', '".$liverpool."', '".$manchesterC."', '".$manchesterU."');";
if(!mysqli_query($con, $sql)){
echo '{"message":"Unable to save the data to the database."}';
die(mysqli_error($con));
}
?>
如果不包含用户 ID,这允许我将数据插入数据库中。 因此,我想知道如何让登录的用户 ID 与额定理由建立有效关系。在我的脑海中,我想我需要从客户端表中选择用户 ID,以便为评级表上的用户 ID 字段插入一个值。但是我不确定,所以第二个意见将不胜感激。 在此先感谢
登录php
<?php
session_start();
include_once 'connection.php';
class User {
private $db;
private $connection;
function __construct() {
$this -> db = new DB_Connection();
$this -> connection = $this->db->getConnection();
}
public function does_user_exist($email,$password)
{
$query = "Select * from tbl_client where email='$email' and password = '$password' ";
$result = mysqli_query($this->connection, $query);
if(mysqli_num_rows($result)>0){
$json['success'] = ' Welcome '.$email;
echo json_encode($json);
mysqli_close($this -> connection);
$_SESSION["userid"] = $loggedInUserId;
}else{
$json['error'] = ' wrong login details';
echo json_encode($json);
mysqli_close($this->connection);
}
}
}
$user = new User();
if(isset($_POST['email'],$_POST['password'])) {
$email = $_POST['email'];
$password = md5($_POST['password']);
if(!empty($email) && !empty($password)){
$user-> does_user_exist($email,$password);
$_SESSION["userid"] = $loggedInUserId;
}else{
echo json_encode("you must type both inputs");
}
}
?>
评价 php
<?php
error_reporting(0);
include_once 'init.php';
session_start();
$userId = $_SESSION["userid"];
$arsenal = $_POST["arsenal"];
$chelsea = $_POST["chelsea"];
$liverpool = $_POST["liverpool"];
$manchesterC = $_POST["manchesterC"];
$manchesterU= $_POST["manchesterU"];
$sql = "INSERT INTO `tbl_rating` (`id`,`userid`,`arsenal`, `chelsea`, `liverpool`,`manchesterC`, `manchesterU`) VALUES (NULL,'".$userId."','".$arsenal."', '".$chelsea."', '".$liverpool."', '".$manchesterC."', '".$manchesterU."');";
if(!mysqli_query($con, $sql)){
echo '{"message":"Unable to save the data to the database."}';
die(mysqli_error($con));
}
?>
【问题讨论】:
-
用户登录后,您可以创建一个会话变量 $_SESSION['userID'] = $userID 然后,在插入评分时,您可以得到登录的用户ID。
-
谢谢我喜欢这种方法,我能理解发生了什么以及为什么。不幸的是它不起作用,所以我一定是在某个地方出错了。