2

Is there any way to loop a session variable with an array value to insert into the database?

$list_application which where I store the array value

You will see in my code that I've directly insert the $list_application and it returns an Array when I printed it out.

if(isset($_POST['submit'])) {
    include('includes/dbconn.php');
    $list_application = $_SESSION['LIST_APPS'];
    $currUser= getenv("username");

    $get_data = "INSERT INTO technologyresults(examdate, technology, prof, eid) VALUES(CURDATE(),'$list_application', '$list_application','$currUser')";
    if($conn->query($get_data) === TRUE ) {
        include('includes/dbconn.php');
    } else {
        echo "Error: " . $get_data. "<br>" . $conn->error;
    }               
    $conn->close();
    header('location: summary.php');
}
Barry
  • 3,303
  • 7
  • 23
  • 42
winter
  • 59
  • 6
  • 1
    Possible duplicate of [PHP how to loop through a post array](https://stackoverflow.com/questions/10262763/php-how-to-loop-through-a-post-array) – E3Im Oct 05 '18 at 08:34
  • Yes, you can loop it, but it's not going to play well with that database structure, I think... – msg Oct 05 '18 at 08:35
  • how does the array look like? should it fill the row in the table? – Guy Louzon Oct 05 '18 at 08:36
  • based on your query, it looks like you could just use `$list_application['examdate']`, `$list_application['technology']`, ... etc. to insert the values. – lovelace Oct 05 '18 at 08:42

1 Answers1

0

if I understood your question correctly, then what yyou need is to implode the array to a string that would match the insert:

$list_application = implode("','", $_SESSION['LIST_APPS']) ;

and in you code, notice I changed the INSERT a bit:

if(isset($_POST['submit'])) { include('includes/dbconn.php');

   $list_application = implode("','", $_SESSION['LIST_APPS']) ;

    $currUser= getenv("username"); $get_data = "INSERT INTO technologyresults(examdate, technology, prof, eid) VALUES(CURDATE(),'".$list_application. "','$currUser')"; if($conn->query($get_data) === TRUE ) { include('includes/dbconn.php'); } else { echo "Error: " . $get_data. "<br>" . $conn->error; } $conn->close(); header('location: summary.php'); }
Guy Louzon
  • 1,175
  • 9
  • 19