【问题标题】:How to get value of a foreign key for a table insert如何获取表插入的外键值
【发布时间】: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。
  • 谢谢我喜欢这种方法,我能理解发生了什么以及为什么。不幸的是它不起作用,所以我一定是在某个地方出错了。

标签: php mysql database


【解决方案1】:

您想查看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。这样您就不需要在插入查询中指定它。

【讨论】:

  • 谢谢你。我已经尝试过了,但错误“无法将数据保存到数据库”无法添加或更新子行:外键失败('db_client'.'tbl_rating',CONSTRAINT'tbl_rating_ibfk_1'外键('userid')
  • 出现我更新的登录 php 可能有错误?我将编辑我的帖子以添加更新的 php 文件。
【解决方案2】:

<?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));
}
?>

【讨论】:

  • 发布代码 - 只回答不好。请发布您的代码的一些解释。
  • 不幸的是,这并不能纠正我的问题,因为 NULL 只会成为我用户 ID 中的值。
猜你喜欢
  • 2016-10-08
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多