본문 바로가기

웹/PHP

POST-OBCJECT

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    
</head>
<body>

<!--POSTMAN 앱: HTTP GET/POST 테스트 앱 -->
<!-- GET/POST(파라미터, 파일전송 기능 ) -->

<!-- 테스트 방법 : localhost/ex31.php -->
    <form method="post" action="ex34_post_bind.php">
        아이디 : <input type="text" name="user_id"> <br />
        비밀번호 : <input type="password" name="user_pw"> <br />
        <input type="submit">
    </form>
</body>
</html>

<?php
    //POST 방식으로 user_id, user_pw를 넘겨서 회원이 있는지 로그인 처리한다
    header('Content-Type: text/html; charset=utf8');
    $connect=mysqli_connect("localhost","root","111111","mydb");
                     // 서버 url 주소, 아이디, 암호 , db이름           
    if(mysqli_connect_errno()){ // db 연결 실패시 
        echo"Failed to connect to MySQL: " .mysqli_connect_errno();
        exit;
    }
    $user_id=$_POST["user_id"];
    $user_pw=$_POST["user_pw"];
   $row_count=0;
   $rows=array();
   //prepare statement
   if($stmt=mysqli_prepare($connect,
                            "SELECT*FROM users WHERE user_id= ? AND user_pw = ?"))
    {   
        // s: string 의 약자
        mysqli_stmt_bind_param($stmt,'ss',$user_id,$user_pw);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt,$col1,$col2,$col3,$col4);
        while(mysqli_stmt_fetch($stmt)){
            // 배열이름 [] =값 
            // array_push()와 동일, 뒤에 추가함 
            $rows[]=array("user_index"=>$col1, "user_id"=>$col2, "user_pw" => $col3, "phone"=>$col4);
            $row_count++;
        }
        mysqli_stmt_close($stmt);
    }    
        //검색된 결과를 JSON 타입으로 반환함 
        if ($row_count >0){
            $json_result=array(
                'result'=>'ok',
                'message'=>'데이타 검색이 성공했습니다.',
                'data' => $rows
            );
        } else{
            $json_result=array(
                'result'=>'fail',
                'message'=>'데이타 검색이 실패했습니다.',
                'data' => $rows
            );
        }
     //json 문자열 생성 
        $json=json_encode(array("result"=>$json_result),
                            JSON_PRETTY_PRINT +JSON_UNESCAPED_UNICODE);  
        // 응답 
        echo $json;
        mysqli_close($connect);
?>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    
</head>
<body>

<!--POSTMAN 앱: HTTP GET/POST 테스트 앱 -->
<!-- GET/POST(파라미터, 파일전송 기능 ) -->

<!-- 테스트 방법 : localhost/ex31.php -->
    <form method="post" action="ex33_post_object.php">
        아이디 : <input type="text" name="user_id"> <br />
        비밀번호 : <input type="password" name="user_pw"> <br />
        <input type="submit">
    </form>
</body>
</html>

<?php
    //ex33_post_object.php

    //POST 방식으로 user_id, user_pw를 넘겨서 회원이 있는지 로그인 처리한다

    header('Content-Type: text/html; charset=utf8');

    $connect=mysqli_connect("localhost","root","111111","mydb");
                        // 서버 url 주소, 아이디, 암호 , db이름  
    if($connect->connect_errno){ // db 연결 실패시 
        echo"Failed to connect to MySQL: " .$connect->connect_errno;
        exit;
    }
    //mySQL의 한글 데이타 깨지지 않도록 문자 인코딩 지정 
    mysqli_set_charset($connect,"utf8");

    $user_id=$_POST["user_id"];
    $user_pw=$_POST["user_pw"];

    //SQL 문 작성 
    $sql ="SELECT * FROM users WHERE user_id='$user_id' AND user_pw='$user_pw' ";

    //SQL 쿼리 전송 (실행)
    $result=$connect->query($sql);

    //검색 된 결과 가져오기 
    // MYSQLI_BOTH: 일반배열 + 연관배열 
    // MYSQLI_NUM: 일반배열
    // MYSQLI_ASSOC: 연관배열

    while( $row =$result->fetch_array(MYSQLI_BOTH)){
        echo $row["user_id"] ."," .$row["user_pw"];
    }

    //결과 값 메모리 해제
    mysqli_free();

    //db 연결 닫기 
    mysqli_close();





?>

' > PHP' 카테고리의 다른 글

JS-PHP  (0) 2019.11.19
JSON  (0) 2019.11.18
이미지 올리기  (0) 2019.11.18
mysql 연동 - user table  (0) 2019.11.18
배열~함수  (0) 2019.11.15