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

≡放荡痞女 提交于 2020-02-04 02:26:30

问题


I am modifiny an existing project by adding a feedback form to it. I need to store feedback form data into a table call feedback_formtb. I code the sql to create this table. And also there is an already created table call profile_request and I want to take a foreign key from this profile_request table. So I add the request_id field as the foreign key.(I have no permission to edit profile_request table because that part is already developed) I crate a file call feedback_test.php.

Now I want to insert feedback form data to the feedback_formtb table. I have done it according to my understanding. But I am not sure whether this sql insert query is correct because of the foreign key and I is this correctly insert data to the table.(I have no user interfaces since i am asking to add this feed back form to the existing project). Really appreciate your help if some one can help me to tell where this is ok. Thanks in advance.

===============feedback_formtb table create===================

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


}


 ?>

回答1:


You don't need the subquery. Just use $request_id as the value of the column.

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

The foreign key constraint will ensure that $request_id is valid. If you try to insert an ID that doesn't exist in profile_requests, this will get an error.



来源:https://stackoverflow.com/questions/59907416/insert-data-into-a-table-in-php-which-has-a-foreign-key-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!