-2

I am having with my query because Insert into value and select is not working, Is this the proper way of using it? thankyou!

This is my query line

$sql = "INSERT INTO `stud_class` (`stud_fullname`, `stud_uid`,`stud_code`, `stud_subject`, `stud_cname`,`stat`)  VALUES ('$stud_full','$stud_uid',(SELECT subject_code,subsubject,class_Name FROM subject WHERE subject_code = '$subcode'),1)";
Jeric Renz
  • 21
  • 6

1 Answers1

0

A subquery that's used as an expression is only allowed to return one value, not multiple columns.

You need to use the SELECT query as the source of all the values, not as an expression inside the VALUES list.

$sql = "INSERT INTO `stud_class` (`stud_fullname`, `stud_uid`,`stud_code`, `stud_subject`, `stud_cname`,`stat`)  
    SELECT '$stud_full','$stud_uid', subject_code,subsubject,class_Name, 1
    FROM subject WHERE subject_code = '$subcode')";

You should also use a prepared statement rather than substituting variables into the SQL string. See How can I prevent SQL injection in PHP?

Barmar
  • 741,623
  • 53
  • 500
  • 612