1

I have a table named 'enroll' with the attributes:

student_id | section | enroll_num      
           |    T4   |      1    
           |    T6   |      2 
           |    L2   |      3                                

enroll_num is the primary key and I have filled in the 'section' field with the code given below that uses values from another table. And it leaves the student_id empty. How would I fill in the NULL student_id field or is there a way to simulaneously fill in student_id as 'section' values are inserted?

if(isset($_POST['add2']))
{
$group = $_POST['group'];
$id = $_POST['id'];

$sql = "INSERT INTO enroll (section)
  SELECT section FROM info3 WHERE group_no LIKE '%$group%'";      

if (mysqli_query($conn, $sql)) 
{
echo "Successfully inserted";
} 
else 
{...

I have looked enough and have tried -

 $sql2 = "UPDATE enroll SET student_id ='$id' WHERE student_id IS NULL";

but it is not working.

  • possible duplicate of [How to set initial value and auto increment in MySQL?](http://stackoverflow.com/questions/1485668/how-to-set-initial-value-and-auto-increment-in-mysql) – Ben Jan 24 '15 at 14:37

2 Answers2

0

An id attribute should never be NULL in the first place, but a unique identifier for the respective database record (row).

But you can update your table with the enroll_num, as this seems to be unique?

"UPDATE enroll SET student_id ='$id' WHERE enroll_num = '$enroll_num'";
player
  • 70
  • 11
0

I think you can insert the student_id within the insert statement, where u have to add student_id ($id) in your select query.

$sql = "INSERT INTO enroll (student_id, section)
        SELECT $id, section FROM info3 WHERE group_no LIKE '%$group%'"; 
fortune
  • 3,361
  • 1
  • 20
  • 30