遍历具有数组值的会话变量

时间:2018-10-05 08:30:31

标签: php mysql arrays

是否有任何方法可以将带有数组值的会话变量循环插入数据库?

$list_application,我在其中存储数组值

您会在我的代码中看到我已经直接插入$ list_application,并且在我打印出它时会返回一个Array

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');
}

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么您需要将数组内插到与插入内容匹配的字符串中:

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

在您的代码中,请注意我对INSERT做了一些更改:

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'); }