我目前正在创建一个基于足球的应用程序。最初,用户需要登录才能使用存储在数据库中的应用程序。此后,将向用户显示主菜单,可以在其中选择一个区域以评分地面。从这里可以将评级保存并存储到数据库中。我当前拥有的数据库包含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为主键,用户ID为外键。
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字段插入一个值。但是,我不确定,因此请您提出第二意见。
提前致谢
登录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));
}
?>
最佳答案
您想查看PHP sessions。这样,您可以将登录用户的ID存储在会话中,并在需要时引用它。因此,您的代码将变为:
无论您在哪里登录:
<?php
session_start();
//... your login code
//... after valid login
$_SESSION["userId"] = $loggedInUserId;
?>
您的其他代码:
<?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."');";
另外,由于
userid
列是自动递增的,因此没有理由将其设置为NOT NULL
。只要给它NULL
。这样,您就无需在插入查询中指定它。