1

I have some sample data like this which is generated from database sample data

I want to insert just single row value by Assign button in the corresponding row E.g. by clicking first-row assign button I want to insert 'CSE412', 'Artificial Intelligence', '3' My problem is when I click any of assign button all of the data which is generated from a database are inserted into the database. My try is

<?php
    $stmt = $conn->prepare("SELECT * FROM tbl_course");
    $stmt->execute();
    $i = 0;
    while($row = $stmt->fetch()) {
      $student_id = $_GET['id'];
      $course_id  = $row['id'];
?>
<form class="" action="" method="post">
<tr style="border-top: 1px solid #32383e;">
  <td><?php echo ++$i; ?></td>
  <td><?php echo $row['course_code']; ?></td>
  <td><?php echo $row['course_title']; ?></td>
  <td><?php echo $row['credit']; ?></td>
  <td>
    <?php
      if ($_SERVER["REQUEST_METHOD"]=="POST"){
        $sql = "INSERT INTO tbl_course_enroll (student_id, course_id) VALUES ('$student_id', '$course_id')";
        $conn->exec($sql);
      }
    ?>
        <input type="submit" name="" value="Assign" class="btn btn-primary">
    </form>
  </td>
</tr>
<?php
  }
  $conn = null;
?>

1 Answer 1

1

First of all, take that if ($_SERVER["REQUEST_METHOD"]=="POST"){ ... } block outside of the while() loop because it's causing repeated INSERT operation. Now the question is, how do you insert a specific row data after clicking the corresponding Assign button? Here's the solution:

  • Change your form's action attribute in the following way,

    <form ... action="?id=<?php echo $student_id; ?>&cid=<?php echo $course_id; ?>" ...>
    
  • Assign a value in the name attribute of Assign button.

    <input type="submit" name="assign" value="Assign" class="btn btn-primary">
    
  • And when the user hits the Assign button, store the corresponding row details in the following way,

    if (isset($_POST['assign'])){
        $stmt = $conn->prepare("INSERT INTO tbl_course_enroll (student_id, course_id) VALUES (?,?)");
        $stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
        $stmt->bindParam(2, $_GET['cid'], PDO::PARAM_STR, 12);
        if($stmt->execute()){
            // INSERT operation successful
        }
    }
    

Here's the complete code,

<?php
    if (isset($_POST['assign'])){
        $stmt = $conn->prepare("INSERT INTO tbl_course_enroll (student_id, course_id) VALUES (?,?)");
        $stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
        $stmt->bindParam(2, $_GET['cid'], PDO::PARAM_STR, 12);
        if($stmt->execute()){
            // INSERT operation successful
        }
    }

    $stmt = $conn->prepare("SELECT * FROM tbl_course");
    $stmt->execute();
    $i = 0;
    while($row = $stmt->fetch()) {
        $student_id = $_GET['id'];
        $course_id  = $row['id'];
?>
    <form class="" action="?id=<?php echo $student_id; ?>&cid=<?php echo $course_id; ?>" method="post">
        <tr style="border-top: 1px solid #32383e;">
            <td><?php echo ++$i; ?></td>
            <td><?php echo $row['course_code']; ?></td>
            <td><?php echo $row['course_title']; ?></td>
            <td><?php echo $row['credit']; ?></td>
            <td>
                <input type="submit" name="assign" value="Assign" class="btn btn-primary">
            </td>
        </tr>
    </form>
<?php
    }
  $conn = null;
?>
Sign up to request clarification or add additional context in comments.

11 Comments

@ShafikShaon, Here I'm assuming that student_id column is of integer type and course_id column is of string type.
Problem arise this will not insert the data redirect to another page
@ShafikShaon What do you mean by data redirect to another page? The form will submit the data to whichever page it's currently in.
suppose my form is in course.php file .From index.php I go to the course.php file. After form submit it will automatically go to index.php
@ShafikShaon It doesn't matter from which page you end up in course.php page. However, if you entire form and corresponding processing logic is in course.php page, then change your form's action attribute in the following way, action="course.php?sid=<?php echo $student_id; ?>&cid=<?php echo $course_id; ?>"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.