2012-05-26 74 views
0

我有這段代碼,它所做的是將數據插入數據庫。我有兩個叫autobox,第二個housebox。當我從下拉列表中選擇auto時,autobox tbody將顯示一個下拉列表和一個文本輸入。薩莫與housebox。 我想要完成的是:當顯示autobox並隱藏housebox,填寫表單然後提交時,housebox將不會插入到數據庫中。 我希望我這個將一個數據塊插入到數據庫中,而其他塊被隱藏

此代碼清楚是不是安全使用,因爲它是:SQL INJECTION

<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唯一。我的意思是,如果'自動',那麼你的input_posts可能會有一個表明它屬於'自動'表。在唯一命名它們之後,您可以在執行查詢之前添加一個條件,如果自動然後使用唯一的「自動」帖子執行查詢,否則如果是房屋,則使用唯一的'house'帖子執行查詢。 –

+0

問題不清楚。我希望你希望在下拉框中選擇auto時插入autobox值,並且housebox將被隱藏,並且這些值不應插入到DB中。如果你選擇housebox反之亦然 – Juice

+0

Limi傑林這正是我想要的。 CH Ri Ri我喜歡你的想法,但我不知道如何開始。 – Rocks

回答

0

你需要得到它的工作最少:

只有插入培訓相關領域:

<?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()); 
} 
?> 

隱藏的非活躍的領域:

<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> 

如果沒有這一個讓我知道。謝謝

相關問題