0
CREATE TABLE `user` (
`user_id` int(50) NOT NULL,
`user_name` varchar(150) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `reports`
ADD PRIMARY KEY (`report_id`),
ADD KEY `cand_id` (`cand_id`) USING BTREE,
ADD KEY `question_id` (`question_id`) USING BTREE,
 ADD KEY `user_id` (`user_id`);

ALTER TABLE `reports`
 ADD CONSTRAINT `reports_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `questions`
(`question_id`),
ADD CONSTRAINT `reports_ibfk_2` FOREIGN KEY (`cand_id`) REFERENCES `candidates` (`cand_id`),
ADD CONSTRAINT `reports_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`);
COMMIT;
#this is sql which i did it in php my admin

function viewUser(){
  $userId = $_GET['id'];

  $sql = "select * from user";
  $query = $this->db->simplequerywithoutcondition($sql);
  $results = $query->fetchAll();
  return $results;

  $sql = "select * from user where user_id = $userId";
  $query = $this->db->simplequerywithoutcondition($sql);
  $results = $query->fetchAll();
  return $results;
}
function viewReport()
{
$candId = $_GET['id'];
$sql = "select * from questions, reports, user , candidates where reports.cand_id = candidates.cand_id and reports.question_id = questions.question_id and reports.cand_id = $candId";
$query = $this->db->simplequerywithoutcondition($sql);
$results = $query->fetchAll();
return $results;

$sql = "select * from user where reports.user_id = user.user_id";
$query = $this->db->simplequerywithoutcondition($sql);
$results = $query->fetchAll();
return $results;
}
#this is a php file named view.php

public function createExam($data){
 if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submitReport'])) {
$candId = $_GET['id'];
$comment = $data['comment'];
$userId = $_GET['id'];
$count = count($data);
for ($x=1; $x < $count; $x++) {
if (!empty($data["questionId$x"])  and !empty($data["result$x"])) {
$questionId = $data["questionId$x"];
$result = $data["result$x"];
$sql = "insert into reports(question_id, cand_id, user_id, result) values(?, ?, ?, ?)";
$arr = array($questionId, $candId, $userId, $result);
$results = $this->db->simplequery($sql, $arr);
}
 }
$sql = "insert into comments(comment, cand_id) values(?, ?)";
$arr = array($comment, $candId);
$results = $this->db->simplequery($sql, $arr);
echo '<div class="alert alert-success"><b>Report store Successfully!</b> You have successfully stored a candidate report.</div>';
}
}
#this is a php file named create.php

So my problem is that I want my user_id to get updated when i take exam and must be saved in reports cloumn. and the user_id must be the same as the current logged in user. whose data is stored in user and he has a user_id. I am a student and I am new to coding and new to stack overflow. Kindly, help me figure out my mistake. Thank you in advance

Error message on the UI:

Fatal error: 
Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 
  Cannot add or update a child row: a foreign key constraint fails (`interview`.`reports`, CONSTRAINT `reports_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)) 
  in C:\xampp\htdocs\interview\inc\classes\DB.php:25 
Stack trace: 
   #0 C:\xampp\htdocs\interview\inc\classes\DB.php(25): PDOStatement->execute(Array) 
   #1 C:\xampp\htdocs\interview\inc\classes\Create.php(87): DB->simplequery('insert into rep...', Array) 
   #2 C:\xampp\htdocs\interview\takeExam.php(122): Create->createExam(Array) 
   #3 {main} thrown in C:\xampp\htdocs\interview\inc\classes\DB.php on line 25
meewog
  • 1,654
  • 1
  • 23
  • 26
  • Why do you not post the error message as text ? (see: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question)) – Luuk Feb 17 '22 at 15:05
  • @Luuk Thank you for the information. I have now edited my question. Let me know if I have to make any further changes. :-) – Pessimist God Feb 17 '22 at 15:16
  • Do you know about [foreign keys](https://dev.mysql.com/doc/mysql-tutorial-excerpt/8.0/en/example-foreign-keys.html)? If not, you can always do this: [disable them](https://stackoverflow.com/a/33238549/724039) – Luuk Feb 17 '22 at 15:27

0 Answers0