Foreach loop with multiple arrays

前端 未结 7 1854
南旧
南旧 2021-01-01 03:50

This is what I want:


foreach($_POST[\'something\'] as $something){
    foreach($_POST[\'example\'] as $example){
        $query = mysq         


        
7条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 04:28

    Your solution does not seem to send the data twice. Unless if records with the same values appear as a result of issuing your queries. This might mean that you should process your data before constructing your queries.

    One solution could be:

    $sql = array();
    
    foreach($_POST['something'] as $something){
        foreach($_POST['example'] as $example){
        $sql[] = "INSERT INTO table (row, row2) VALUES ('{$something}','{$example}')";
    }
    }
    
    foreach($sql as $query){
      mysql_query($query);
    }
    

提交回复
热议问题