2012-05-26 4 views
0

이 코드는 데이터베이스에 데이터를 삽입하는 것입니다. 나는 두 개가 autobox, 두 번째 하우스 박스라고 불린다. 드롭 다운 목록에서 auto를 선택하면 autobox tbody가 드롭 다운 목록과 텍스트 입력으로 표시됩니다. 사모와 하우스. 내가하려고하는 것은 : autobox가 표시되고 housebox가 숨겨져 있고 양식을 채운 다음 housebox를 제출하면 데이터베이스에 삽입되지 않습니다. 나는이다른 블록이 숨겨져있는 동안 하나의 데이터 블록을 데이터베이스에 삽입하십시오.

이 코드에 대해 내가 분명히 희망하는 사용하는 것이 안전하지 않습니다 그것은 그대로 : SQL 인젝션

<script> 
    function addSubject(){ 
    selectedSubject = document.getElementById('subcategory').value 
    if (selectedSubject == 'auto'){ 
    document.getElementById('autobox').style.display = 'block'; 

}else if (selectedSubject == 'house'){ 
    document.getElementById('housebox').style.display = 'block'; 
    } 
    } 
    </script> 

    <?php  
    if(isset($_POST['upload'])){ 
    $title = $_POST['title']; 
    $description = $_POST['description']; 
    $price = $_POST['price']; 
    $subcategory = $_POST['subcategory']; 
    $mileage = $_POST['mileage']; 
    $make = $_POST['make']; 


    $query = "INSERT INTO classifieds (id, subcategory, title, description, mileage, 

    make, price, broom) VALUES ('', $subcategory, '$title', '$description', 

    '$mileage', '$make', '$price', '$broom')"; 
    mysql_query($query) or die(mysql_error()); 

    } 
    ?> 

    <form action="insert.php" method="post" name="insert" enctype="multipart/form- 

    data"> 

    <select name="subcategory" id="subcategory" onchange="addSubject()" > 
    <option value="">Select Manufacturer</option> 
    <option value="auto">Auto</option> 
    <option value="house">House</option> 
    </select> 
    <span class="style64">Title</span> 
     <input type="text" name="title" class="input"/> 

    <table> 
    <tbody class="autobox" id="autobox" style="display: none;" > 
    <tr> 
    <td class="title">Enter mileage:</td> 
    <td class="field"> 
    <input type="text" name="mileage" size="8" maxlength="7" /></td> 
    </tr> 
    <tr> 
    <td> 
    <span>Select Manufacturer : </span> 

    <select name="make"> 
    <option value="Ford">Ford</option> 
    <option value="Chevrolet">Chevrolet</option> 
    <option value="Audi">Audi</option> 
     </select> 

</td> 
</tr> 
</tbody> 

<tbody class="housebox" id="housebox" style="display: none;" > 
<tr> 
<td class="title">Enter Price:</td> 
<td class="field"> 
    <input type="text" name="price" size="8" maxlength="7" /></td> 
    </tr> 
    <tr> 
    <td> 

    <select name="broom"> 
<option value="1b">1 broom</option> 
<option value="2b">2 broom</option> 
<option value="3b">3 broom</option> 
     </select> 

    </td> 
    </tr> 
    </tbody> 
    </table> 

    <textarea name="description" rows="5" class="input"></textarea> 
    <input type="submit" name="upload" value="Continue" /> 
        </form> 

감사

+0

당신이 당신의 이름을 지정 시도가 iput_posts는 고유합니다. 내 말은 'auto'이면 input_posts가 'auto'테이블에 속한다는 표시가있을 수 있습니다. 고유 한 이름을 지정한 후 쿼리를 실행하기 직전에 조건을 추가 할 수 있습니다. 자동으로 고유 한 '자동'게시물로 쿼리가 실행되고, 집에있는 경우 고유 한 '집'게시물로 쿼리가 실행됩니다. –

+0

질문이 명확하지 않습니다. 나는 당신이 드롭 다운 상자에서 housebox가 숨겨져 있고 그 값이 DB에 삽입되어서는 안되는 자동이 선택되었을 때 autobox 값을 삽입하고 싶습니다. 집방을 선택하는 경우 – Juice

+0

Limi Jerin 그것이 내가 원하는 것입니다. 치 리오 당신의 아이디어가 마음에 들지만, 어떻게 시작 해야할지 모르겠습니다. – Rocks

답변

0

당신이 그것을 작동하는 데 필요한 최소한 감사

<script> 
function addSubject(){ 
    selectedSubject = document.getElementById('subcategory').value; 
    if (selectedSubject == 'auto'){ 
    document.getElementById('autobox').style.display = 'block'; 
    document.getElementById('housebox').style.display = 'none'; 
    }else if (selectedSubject == 'house'){ 
    document.getElementById('housebox').style.display = 'block'; 
    document.getElementById('autobox').style.display = 'none'; 
    } 
} 
</script> 
1

이보십시오. 이것은 당신이없는이 일이 알려 경우

코드

<script> 
    function addSubject(){ 
    selectedSubject = document.getElementById('subcategory').value 
    if (selectedSubject == 'auto'){ 
    document.getElementById('autobox').style.display = 'block'; 
    document.getElementById('housebox').style.display = 'none'; 
}else if (selectedSubject == 'house'){ 
    document.getElementById('housebox').style.display = 'block'; 
    document.getElementById('autobox').style.display = 'none'; 
    } 
    } 
    </script> 

    <?php  
    if(isset($_POST['upload'])){ 
    $title = $_POST['title']; 
    $description = $_POST['description']; 
    $price = $_POST['price']; 
    $subcategory = $_POST['subcategory']; 
    $mileage = $_POST['mileage']; 
    $make = $_POST['make']; 
    if($subcategory=='auto') 
    { 
       $query = "INSERT INTO classifieds (id, subcategory, title, description, mileage, make, price, broom) VALUES ('', $subcategory, '$title', '$description', 

     '$mileage', '$make', '', '')"; 
    } 
    else 
    { 
     $query = "INSERT INTO classifieds (id, subcategory, title, description, mileage, make, price, broom) VALUES ('', $subcategory, '$title', '$description', 

     '', '', '$price', '$broom')"; 
    } 
    mysql_query($query) or die(mysql_error()); 
    } 
    ?> 

    <form action="insert.php" method="post" name="insert" enctype="multipart/form-data"> 

    <select name="subcategory" id="subcategory" onchange="addSubject()" > 
    <option value="">Select Manufacturer</option> 
    <option value="auto">Auto</option> 
    <option value="house">House</option> 
    </select> 
    <span class="style64">Title</span> 
     <input type="text" name="title" class="input"/> 

    <table> 
    <tbody class="autobox" id="autobox" style="display: none;" > 
    <tr> 
    <td class="title">Enter mileage:</td> 
    <td class="field"> 
    <input type="text" name="mileage" size="8" maxlength="7" /></td> 
    </tr> 
    <tr> 
    <td> 
    <span>Select Manufacturer : </span> 

    <select name="make"> 
    <option value="Ford">Ford</option> 
    <option value="Chevrolet">Chevrolet</option> 
    <option value="Audi">Audi</option> 
     </select> 

</td> 
</tr> 
</tbody> 

<tbody class="housebox" id="housebox" style="display: none;" > 
<tr> 
<td class="title">Enter Price:</td> 
<td class="field"> 
    <input type="text" name="price" size="8" maxlength="7" /></td> 
    </tr> 
    <tr> 
    <td> 

    <select name="broom"> 
<option value="1b">1 broom</option> 
<option value="2b">2 broom</option> 
<option value="3b">3 broom</option> 
     </select> 

    </td> 
    </tr> 
    </tbody> 
    </table> 

    <textarea name="description" rows="5" class="input"></textarea> 
    <input type="submit" name="upload" value="Continue" /> 
        </form> 

을 테스트되지 않은 상태입니다 원하는 것입니다 바랍니다. 비활성 필드를 숨기기

<?php  
if(isset($_POST['upload'])){ 
    $title = $_POST['title']; 
    $description = $_POST['description']; 
    $price = $_POST['price']; 
    $subcategory = $_POST['subcategory']; 

    if ($subcategory == 'auto'){ 
     $mileage = $_POST['mileage']; 
     $make = $_POST['make']; 
     $query = "INSERT INTO classifieds (subcategory, title, description, mileage, make, price) VALUES ($subcategory, '$title', '$description', '$mileage', '$make', '$price')"; 
    } elseif ($subcategory == 'house') { 
     $broom = $_POST['broom']; 
     $query = "INSERT INTO classifieds (subcategory, title, description, broom, price) 
       VALUES ($subcategory, '$title', '$description', '$price')"; 
    } 

    mysql_query($query) or die(mysql_error()); 
} 
?> 

:

가 만와 관계있는 필드를 삽입 : 당신

관련 문제