我想要做的是当用户为活动提供地点信息时。单击按钮后,它会正确地将数据保存到场地表中,然后转到活动创建的位置。现在,当给出以下信息时,我需要将 venue.id放入活动表。此外,还有另一个来自用户表的外键,名为Activity_Manager_User_SSN。此外键是用户定义的。我需要这两个记录。我的Php知识较少。我怎样才能获得这个场地。想进入活动表。我在android中使用retrofit2
我的数据库中有多个表,如下所示:
CREATE TABLE IF NOT EXISTS `activityplanner_db`.`Activity` (
`Id` INT NOT NULL,
`Activity_Name` VARCHAR(45) NOT NULL,
`Activity_StartDate` DATETIME NOT NULL,
`Activity_EndDate` DATETIME NOT NULL,
`Activity_Attachment` VARCHAR(45) NOT NULL,
`Activity_Capacity` INT NOT NULL,
`Activity_City` VARCHAR(45) NOT NULL,
`Activity_Website` VARCHAR(45) NOT NULL,
`Venue_Id` INT NULL,
`Activity_Manager_User_SSN` INT NULL,
PRIMARY KEY (`Id`),
INDEX `Venue_Id_idx` (`Venue_Id` ASC),
INDEX `Activity_Manager_User_SSN_idx` (`Activity_Manager_User_SSN` ASC),
CONSTRAINT `Venue_Id`
FOREIGN KEY (`Venue_Id`)
REFERENCES `activityplanner_db`.`Venue` (`Id`)
CONSTRAINT `Activity_Manager_User_SSN`
FOREIGN KEY (`Activity_Manager_User_SSN`)
REFERENCES `activityplanner_db`.`User` (`SSN`)
)
CREATE TABLE IF NOT EXISTS `activityplanner_db`.`Venue` (
`Id` INT NOT NULL,
`Venue_Address` VARCHAR(45) NULL,
`Venue_Name` VARCHAR(45) NULL,
`Venue_City` VARCHAR(45) NULL,
PRIMARY KEY (`Id`))
活动也有自动增量.Id和venue.Id。
在我的应用程序中,我使用php发布daha,如下所示:
<?php
require_once ('Connection.php');
class insertvenue{
function startInsertvenue()
{
$connection = new Connection();
$conn = $connection->getConnection();
$response = array();
$VenueAddress = $_POST['Venue_Address'];
$VenueName = $_POST['Venue_Name'];
$VenueCity = $_POST['Venue_City'];
try{
if(isset($VenueName)){
$sqlInsert = "INSERT INTO venue (Venue_Address, Venue_Name, Venue_City) VALUES ('$VenueAddress', '$VenueName', '$VenueCity')";
$conn -> exec($sqlInsert);
}
}catch (PDOException $exception){
echo "Mekan kaydı yapılamadı".$exception->getMessage();
}
if($sqlInsert){
//success inserted
$response["success"] = 1;
$response["message"] = "Venue successful inserted!";
echo json_encode($response);
}else{
//failed inserted
$response["success"] = 0;
$response["message"] = "Failed while insert data";
echo json_encode($response);
}
}
}
$insert = new insertvenue();
$insert->startInsertvenue();;
?>
<?php
require_once ('Connection.php');
class insertactivity{
function startInsertActivity()
{
$connection = new Connection();
$conn = $connection->getConnection();
$response = array();
$ActivityName = $_POST['Activity_Name'];
$ActivityStartDate = $_POST['Activity_StartDate'];
$ActivityEndDate = $_POST['Activity_EndDate'];
$ActivityCapacity = $_POST['Activity_Capacity'];
$ActivityCity = $_POST['Activity_City'];
$Venue_Id = $_POST['Venue_Id'];
try{
if(isset($ActivityName)){
$sqlInsert = "INSERT INTO activity (Activity_Name, Activity_StartDate, Activity_EndDate, Activity_Capacity,
Activity_City, Venude_Id)
VALUES ('$ActivityName','$ActivityStartDate',' $ActivityEndDate', '$ActivityCapacity',
'$ActivityCity', '')";
$conn -> exec($sqlInsert);
}
}catch (PDOException $exception){
echo "fail".$exception->getMessage();
}
if($sqlInsert){
//success inserted
$response["success"] = 1;
$response["message"] = "Activity successful inserted!";
echo json_encode($response);
}else{
//failed inserted
$response["success"] = 0;
$response["message"] = "Failed while insert activity";
echo json_encode($response);
}
}
}
$insert = new insertactivity();
$insert->startInsertActivity();;
?>
答案 0 :(得分:0)
首先插入场地并获取最后插入的ID:
$vid = $conn->lastInsertId();
然后插入包含$ vid的事件作为您的FK。仅当您保持id自动递增时,这才有效。
编辑: 将您的功能更改为:
function startInsertvenue ($activity_Manager_User_SSN, $user_id){
........
}
并在从用户表中查询后调用它,如下所示:
$insert->startInsertActivity($Activity_Manager_User_SSN, $_SESSION['user_id']);