php
  • html
  • mysql
  • html5
  • 2014-04-16 7 views 0 likes 
    0

    등록 양식이 있습니다. 양식 입력 필드에 데이터를 검색하기 위해 mySQL 쿼리를 사용합니다.php mySQL 데이터베이스에서 데이터를 양식으로 검색합니다.

    이 매우 긴 형식의 일부입니다

    나는 폼의 입력 필드에로드 할 자궁강 데이터를 설정하는 방법
    <form class="form-horizontal" id="registration" method='post' action='ConnectDB.php' onsubmit='return ValidateForm(this);'> 
        <fieldset> 
         <h1 style="margin-top: px;float:right; color:#62A1D5"><br><b>registration</b></h1> 
         <table id="TBackGround"> 
          <td style="padding-left:10px; padding-right:10px;"> 
           <p id="pd" style="margin-top:5px; margin-right:2px; font-size:16px; text-decoration:underline"><b><br>details:</b><p> 
           <p style="line-height: 1.3em" class="details"> 
           <div class="control-group"> 
            <label class="control-label">ID </label> 
            <input type="text"name="studentID" align= "right" class="textbox" ><span id="errorID"></span> <br/> 
           </div> 
           <div class="control-group"> 
            <label class="control-label" >First name</label> 
            <input type="text" name="Fname" class="textbox" ><span id="errorFirstName"> <br/> 
           </div> 
    </form> 
    

    ? 내가 가지고있는 레코드의 ID를 검색하는 쿼리가 있지만 필드에 전체 쿼리 결과를 설정하는 방법을 모르겠습니다.

    내 PHP 쿼리 :

    <?php 
    
    if(isset($_POST['submit'])) 
    { 
    $query = $_POST['query']; 
    $min_length = 1; 
    if(strlen($query) >= $min_length) 
    { 
    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query); 
    echo "<table border='0' width='300' align='center' cellpadding='1' cellspacing='1'>"; 
    echo "<tr align='center' bgcolor='#002C40'> 
    ?> 
    <td height='35px' width='150px'>id</td> <td>first name</td> 
    
    </tr>"; 
    $raw_results = 
    
    mysql_query("SELECT * FROM student WHERE (`idStudent` LIKE '%".$query."%') OR (`FirstName` LIKE '%".$query."%')"); 
    if(mysql_num_rows($raw_results) > 0) 
    { 
    while($results = mysql_fetch_array($raw_results)) 
    { 
    
    echo "<tr align='center' bgcolor='#0f7ea3'> 
    
    <td height='25px'> " 
    .$results['idStudent']."</td> <td>".$results['FirstName']."</td> 
    
    
    
    
    
    
    </tr>" ; 
    } 
    
    } 
    else{ 
    echo "<tr align='center' bgcolor='#6C0000'> 
    
    <td colspan='2' height='25px'>No results</td><tr>"; 
    echo "</table>"; 
    } 
    } 
    else{ 
    echo "Minimum length is ".$min_length; 
    }} 
    
    ?> 
    
    +0

    뜻 : 제안, 자동 완성? 당신은 AJAX가 필요합니다. – raiserle

    +0

    예제를 얻을 수 있습니까? 아주 간단한 예제도 있습니까? – user2674835

    +0

    @ user2674835 : 무슨 뜻인가요? 제안의 예, 자동 완성? – raiserle

    답변

    0

    OK, 기본!

    입력 요소가있는 양식이 있습니다. 양식을 서버로 보내면 서버 측 스크립트가 양식 요소의 값을 가져올 수 있습니다.

    봐이 :

    서버 측 스크립트에
    <form action="the_script_location.php" method="post"> 
    <input type="text" name="element1" /> 
    <input type="submit" name="send" /> 
    </form> 
    

    지금과 같은 형태의 값을 얻을 수 있습니다 :

    <form action="the_script_location.php" method="post"> 
    <input type="text" name="element1" value="<?php isset($row[ 'your_filed_in_database' ]) ? $row[ 'your_filed_in_database' ] : '' ?>" /> 
    <input type="submit" name="send" /> 
    </form> 
    
    :

    <?php 
    if(isset($_POST[ 'send' ])){ 
        $var = $_POST[ 'element1' ]; 
        //now you can use the var for an query to your database 
        //please note: this very basic, without any security of injection 
        $res = mysql_query('SELECT `any` FROM `anyTable` WHERE `any` LIKE \'%'.$var.'%\''); 
        if(mysql_num_row($res){ 
         $row = mysql_fetch_assoc($res); //get one (first) result 
        } 
    } 
    ?> 
    

    이제 양식을 업데이트 할 수 있습니다

    +0

    예를 들어 주셔서 감사합니다! 그러나 쿼리 양식을 내 양식에 전달하는 방법을 이해하는 데 여전히 어려움이 있습니다. PHP 코드가 완료된 후 원하는 페이지를 여는 코드가 아니어야합니까? – user2674835

    0

    PHP 파일에서 양식의 레이아웃을 출력해야합니다. 여기 작은 예가 도움이 될 수 있습니다.

    파일 register.php

    if (!isset($_POST['submit'])) { 
        mysql_connect(); 
        $res = mysql_query("SELECT * FROM users WHERE user_id=1"); 
        $user = array_map('htmlspecialchars', mysql_fetch_assoc($res)); 
    
        echo <<<CUT 
    <form action="register.php" method="post" > 
        <lable>Name:</label> <input type="text" name="name" value="{$user['name']}" /> 
        <lable>Country:</label> <input type="text" name="name" value="{$user['country']}" /> 
        <input type="submit" name="submit" value="Submit" /> 
    </form> 
    CUT; 
    } else { 
        //handle submited data here 
    } 
    
    관련 문제