2026, 새로운 도약을 시작합니다.

PHP 다중 배열 질문 드립니다. 채택완료

다중 배열을 보내고 받으려고 하고 있습니다.

50명 정도를 보내면 받는 파일쪽의 데이터의 저장 mysql에서 뒷 부분이 몇개가 저장이 되지 않는  부분이 발생하고 있습니다. 아직 초보라 다중 배열 보내는 방법에 어려움이 있습니다.

고수님들이 좋은 방법있으면 조언 부탁드립니다.

< 보내는 파일 : friend.php >

<?php
$stu = array('a1','a2','a3','a4','a5','a6','a7','a8','a9','a10');
$cnt=count($stu);
?>

<form action="friend_post.php" method="post" >
<table border=1>
    <tr>
        <td>이름</td>
        <?php for($i=0;$i<$cnt;$i++){ ?>
        <td><?=$stu[$i]?></td>
        <?php } ?>
    </tr>
    <tr>
    <td><?=$stu[0]?></td>
<?php 
    $a=0;
    for($i=1;$i<$cnt*$cnt+1;$i++){ 
    ?>
        <td><input type="number" name="friend[]" min="1" max="9" ></td>
        <?php if($i%$cnt==0){
            $a++;
            echo "</tr><tr>";
            echo "<td> $stu[$a] </td>";    
        } ?>
<?php }     ?>
    </table>
<div><input type="submit"></div>
</form>

< 받는 파일 : friend_postphp >

<?php
for($i=0; $i<count($_POST['friend']); $i++){
    $friend = $_POST['friend'];
}

foreach($friend as $value) {
    $friend .= ','.$value;
}

$txt = explode(',',$friend);
echo "<table border=1><tr>";
$cnt_a=count($txt);
$row=$cnt_a/10;
for($i=1; $i<=$cnt_a; $i++) {
    echo "<td>$txt[$i]<td>";
    if($i%$row==0) {
        echo "</tr><tr>";
    }    
}

echo "</tr></table>";
?>

답변 2개

채택된 답변
+20 포인트

for($i=0; $i<count($_POST['friend']); $i++){
    $friend = $_POST['friend'];
}

foreach($friend as $value) {
    $friend .= ','.$value;
}

$txt = explode(',',$friend);
 

=>

$txt = $_POST['friend'];

DB 처리하는 부분이 없네요.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

db 부분은 일부러 제거하고 올렸습니다.

보내는 부분에서 다른 방법을 찾고 싶습니다. 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고