【问题标题】:Insert Data into a table in php which has a foreign key field将数据插入到具有外键字段的php表中
【发布时间】:2020-01-25 08:01:41
【问题描述】:

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

现在我想将反馈表单数据插入到 feedback_formtb 表中。我按照我的理解做了。但是由于外键,我不确定这个 sql 插入查询是否正确,我是否正确地将数据插入到表中。(我没有用户界面,因为我要求将此反馈表单添加到现有项目中)。 如果有人可以帮助我判断这在哪里可以,非常感谢您的帮助。提前致谢。

===============feedback_formtb 表创建===================

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 表=================

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


}


 ?>

【问题讨论】:

  • 什么是$_JSON
  • ( SELECT id FROM profile_requests WHERE id = ? ) 没有意义。其结果将与参数的 ID 相同。
  • @RamRaider,我通过查看这个项目中的另一个文件来编辑它,以便它使用这个 $_JSON 并要求我按原样使用它
  • @Barmar,感谢您的回复,是的,我使用 (SELECT id FROM profile_requests WHERE id = ?) 来引用 profile_requests 表的外键。我在 select 中使用 id ,因为我不确定将其用作(SELECT request_id FROM profile_requests WHERE id = ?)是否正确,
  • 您根本不需要子查询。只需将$request_id 直接插入列中即可。

标签: php mysql foreign-keys bindparam


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多