Insert Data into a table in php 其中有一个外键字段

Insert Data into a table in php which has a foreign key field

我正在通过向其中添加反馈表来修改现有项目。我需要将反馈表数据存储到 table 调用 feedback_formtb 中。我编码 sql 来创建这个 table。还有一个已经创建的 table 调用 profile_request 我想从这个 profile_request table。所以我添加了 request_id 字段作为外键。(我无权编辑 profile_request table 因为那部分已经开发了) 我创建了一个文件调用 feedback_test.php

现在我想将反馈表数据插入 feedback_formtb table。我按照我的理解做了。但是我不确定这个 sql 插入查询是否正确,因为外键,我是否正确地将数据插入 table。(我没有用户界面,因为我要求添加这个提要返回现有项目的形式)。 如果有人可以帮助我告诉我哪里可以,真的很感谢您的帮助。提前致谢。

===============feedback_formtb table 创建===================

DROP TABLE IF EXISTS `feedback_formtb`;
 CREATE TABLE IF NOT EXISTS `feedback_formtb` (
 `fid` int(10) NOT NULL,
 `job_complete` tinyint(2) NOT NULL,
 `satisfaction` double NOT NULL,
 `reason` int(20) NOT NULL,
 `comment` text NOT NULL,
 `request_id` int(10) NOT NULL,
  PRIMARY KEY (`fid`),
  FOREIGN KEY (`request_id`) REFERENCES profile_requests(`id`)
 )ENGINE=InnoDB DEFAULT CHARSET=latin1;

=============profile_requests Table=================

DROP TABLE IF EXISTS `profile_requests`;
CREATE TABLE IF NOT EXISTS `profile_requests` (
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `created_by` int(10) UNSIGNED NOT NULL,
  `updated_by` int(10) UNSIGNED NOT NULL,
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(10) UNSIGNED NOT NULL,
  `profile_id` int(10) UNSIGNED NOT NULL,
  `expected_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `lat` float UNSIGNED NOT NULL,
  `lng` float UNSIGNED NOT NULL,
  `city_id` int(11) NOT NULL,
  `message` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `state` tinyint(3) UNSIGNED NOT NULL DEFAULT 1 COMMENT '1:new request, 2:accepted,3:rejected',
  `urgent` tinyint(3) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=69 DEFAULT CHARSET=latin1;

=================feedback_test.php=================

<?php 
require_once 'auth.php';

// assigning values

$id = $_JSON['fid'] ?? NULL;
$request_id = $_JSON['$request_id'] ?? NULL;

$job_complete = $_JSON['job_complete'] ?? NULL;
$satisfaction = $_JSON['satisfaction'] ?? NULL;
$reason = $_JSON['reason'] ?? NULL;
$comment = $_JSON['comment'] ?? NULL;


$success = TRUE;  


$submit = $_JSON['submit'] ?? NULL;
if ($submit !== NULL) { // if submit success
    if ($job_complete === NULL) { // if job_complete fails
        echo json_encode(['error' => 'job_complete not provided']);
        die;
    }else if ($satisfaction === NULL) {  // if satisfaction fails
        echo json_encode(['error' => 'satisfaction not provided']);
        die;
    }else if ($reason === NULL) { //if reason fails
        echo json_encode(['error' => 'job_complete not provided']);
        die;
    }else if ($comment === NULL) { //if comment fails
        echo json_encode(['error' => 'job_complete not provided']);
        die;
    }

    // Insert Data 


     $ips = $mysqli->prepare('INSERT INTO feedback_formtb (job_complete, satisfaction, reason, comment, request_id) VALUES (?, ?, ?, ?, ( SELECT id FROM profile_requests WHERE id = ? ))'); 
     $ips->bind_param('idisi', $job_complete, $satisfaction, $reason, $comment, $request_id);

     if($ips->execute()){
            $success = TRUE;
     }if (!$ips->execute()) {
        echo json_encode(['error' => 'Fail to submit']);
        die;
     }


}


 ?>

您不需要子查询。只需使用 $request_id 作为列的值。

 $ips = $mysqli->prepare('
    INSERT INTO feedback_formtb (job_complete, satisfaction, reason, comment, request_id) 
    VALUES (?, ?, ?, ?, ?)'); 

外键约束将确保$request_id有效。如果您尝试插入 profile_requests 中不存在的 ID,则会出现错误。