mardi 11 août 2020

Inserting rows with 'Checked' boxes into another table php

basically, each row in the table has a checkbox

If the checkbox is checked then it should insert the user_id field from the checked rows and the value from a drop down (class_id) at the top of a page into another table.

I have managed the code up to here but I need a tutorial or guidance to do the rest.

At the moment I am getting error HTTP 500

I understand that the code is subject to sql injection, but will sort that out later

<?php
  $servername = "xxx";
  $username = "xxxx";
  $password = "xxxx";
    
  $dbname = "web216-admin-6d5";

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
  }

  $class_id = $_POST['class_id'];  // This value is from a dropdown
  $checkbox = $_POST['checkbox'];   // This is a checkbox next to each record
  $user_id = $_POST['user_id']; // this is a hidden value output from the database (in a textbox)

  if(isset($_POST['submit']))

  {  
  for($i=0;$i<count($checkbox);$i++){

 $query="INSERT INTO Class_List(user_id,class_id)VALUES('".$user_id[$i]."','".$class_id."')";
 if (mysqli_query($conn, $query)) {
     
       echo "The selected records have been added successfully !";
       echo "<meta http-equiv=\"refresh\" content=\"2;url=search_courses.php\"/>";

     } else {
        echo "Error: " . $sql . ":-" . mysqli_error($conn);
     }
     mysqli_close($conn);
}
?>



Aucun commentaire:

Enregistrer un commentaire