PHP从表单而不是输入的信息传递默认值

时间:2015-06-24 21:25:39

标签: php mysql forms

我通过一组隐藏字段从MySQL传递值,以便以单独的形式预填充字段。当用户进行编辑时,我显然希望那些覆盖数据库中的内容。就目前而言,我想要的表单值是预先填充正确的字段,没有任何覆盖,我没有收到任何错误消息。我认为它会自动采取手动输入的内容而不是value =“”属性中显示的内容,但它的行为就好像完全相同的值被重新传递一样。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<?php
//Connect to DB
require_once("classes/database.php");

//Delete item from Database
if (isset($_POST['delete']) && isset($_POST['id'])) {
    $id = get_post('id');
    $query = "DELETE FROM items WHERE ID='$id'";
    if(!mysqli_query($con, $query)) {
        echo "DELETE failed: $query<br />" . mysqli_error() . "<br /><br />";
    }
}
//Move to Pack from Closet
if (isset($_POST['toPack']) && isset($_POST['id'])) {
    $id = get_post('id');
    $query = "UPDATE items SET Status=1 WHERE ID='$id'";
    if(!mysqli_query($con, $query)) {
        echo "UPDATE failed: $query<br />" . mysqli_error() . "<br /><br />";
    }
}

//Move to Closet from Pack
if (isset($_POST['toCloset']) && isset($_POST['id'])) {
    $id = get_post('id');
    $query = "UPDATE items SET Status=0 WHERE ID='$id'";
    if(!mysqli_query($con, $query)) {
        echo "UPDATE failed: $query<br />" . mysqli_error() . "<br /><br />";
    }
}
//Modify Item if Save button has been clicked below
if (isset($_POST['Overwrite']) && isset($_POST['id'])) {
    $id             = get_post('id');
    $type           = get_post('Type');
    $name           = get_post('ItemName');
    $pounds         = get_post('Pounds');
    $ounces         = get_post('Ounces');
    $description    = get_post('Description');
    $url            = get_post('Url');
    echo $description;

    $query = "UPDATE items SET Type='$type', Name='$name', Pounds='$pounds', Ounces='$ounces', Description='$description', Url='$url' WHERE ID='$id'";
    if(!mysqli_query($con, $query)) {
        echo "UPDATE failed: $query<br />" . mysqli_error() . "<br /><br />";
    }
}
//Show Modify form if Modify button has been clicked
if (isset($_POST['Modify']) && isset($_POST['id'])) {

    $type_output = $_POST['Type'];
?>
    <h2>Modify Item</h2>
    <form action='index.php' method='post'>
            <p>Name:    <input type="text" name="itemName" value="<?php echo $_POST['ItemName'] ?>"></p>
            <p>Group:   <select name="Type">
                            <option value="Backpack" <?php if($type_output=="Backpack") { echo "selected"; } ?>>Backpack</option>
                            <option value="Shelter" <?php if($type_output=="Shelter") { echo "selected"; } ?>>Shelter</option>
                            <option value="Sleep System" <?php if($type_output=="Sleep System") { echo "selected"; } ?>>Sleep System</option>
                            <option value="Cooking" <?php if($type_output=="Cooking") { echo "selected"; } ?>>Cooking</option>
                            <option value="Food" <?php if($type_output=="Food") { echo "selected"; } ?>>Food</option>
                            <option value="Rain Protection" <?php if($type_output=="Rain Protection") { echo "selected"; } ?>>Rain Protection</option>
                            <option value="Footwear" <?php if($type_output=="Footwear") { echo "selected"; } ?>>Footwear</option>
                            <option value="Electronics" <?php if($type_output=="Electronics") { echo "selected"; } ?>>Electronics</option>
                            <option value="Toiletries" <?php if($type_output=="Toiletries") { echo "selected"; } ?>>Toiletries</option>
                            <option value="Water Treatment and Storage" <?php if($type_output=="Water Treatment and Storage") { echo "selected"; } ?>>Water Treatment and Storage</option>
                            <option value="First Aid" <?php if($type_output=="First Aid") { echo "selected"; } ?>>First Aid</option>
                            <option value="Clothing" <?php if($type_output=="Clothing") { echo "selected"; } ?>>Clothing</option>
                            <option value="Snow/Cold Weather Gear" <?php if($type_output=="Snow/Cold Weather Gear") { echo "selected"; } ?>>Snow/Cold Weather Gear</option>
                            <option value="Miscellaneous" <?php if($type_output=="Miscellaneous") { echo "selected"; } ?>>Miscellaneous</option>
                            <option value="Luxury" <?php if($type_output=="Luxury") { echo "selected"; } ?>>Luxury</option>
                            <option value="Navigation" <?php if($type_output=="Navigation") { echo "selected"; } ?>>Navigation</option>
                        </select>
        <p>Pounds:      <input type="text" name="Pounds" value="<?php echo $_POST['Pounds'] ?>"></p>
        <p>Ounces:      <input type="text" name="Ounces" value="<?php echo $_POST['Ounces'] ?>"></p>
        <p>Description: <textarea name="Description"><?php echo $_POST['Description']; ?></textarea>
        <p>Url:         <input type="text" name="Url" value="<?php echo $_POST['Url']; ?>"></p>
                        <input type="hidden" name="Overwrite" value="yes">
                        <input type="submit" value="Save">
    </form>
<?php
}
else
{ //Add Item to Closet
    if (isset($_POST['Type']) && isset($_POST['ItemName']) && isset($_POST['Pounds']) && isset($_POST['Ounces']) && isset($_POST['Description']) && isset($_POST['Url'])) {

        $type           = get_post('Type');
        $name           = get_post('ItemName');
        $pounds         = get_post('Pounds');
        $ounces         = get_post('Ounces');
        $description    = get_post('Description');
        $url            = get_post('Url');

        $query = "INSERT INTO items VALUES ('', '', '$type', '$name', '$pounds', '$ounces', '$description', '$url')";

        if(!mysqli_query($con, $query)) {
            echo "INSERT failed: $query<br />" . mysqli_error($con) . "<br /><br />";
        }
    }
    echo    "<h2>Add Item</h2>
                <form action='index.php' method='post'>
                <p>Name: <input type='text' name='ItemName'></p>
                <p>Group:   <select name='Type'>
                                <option value='Backpack'>Backpack</option>
                                <option value='Shelter'>Shelter</option>
                                <option value='Sleep System'>Sleep System</option>
                                <option value='Cooking'>Cooking</option>
                                <option value='Food'>Food</option>
                                <option value='Rain Protection'>Rain Protection</option>
                                <option value='Footwear'>Footwear</option>
                                <option value='Electronics'>Electronics</option>
                                <option value='Toiletries'>Toiletries</option>
                                <option value='Water Treatment and Storage'>Water Treatment and Storage</option>
                                <option value='First Aid'>First Aid</option>
                                <option value='Clothing'>Clothing</option>
                                <option value='Snow/Cold Weather Gear'>Snow/Cold Weather Gear</option>
                                <option value='Miscellaneous'>Miscellaneous</option>
                                <option value='Luxury'>Luxury</option>
                                <option value='Navigation'>Navigation</option>
                            </select></p>
                <p>Pounds: <input type='text' name='Pounds'></p>
                <p>Ounces: <input type='text' name='Ounces'></p>
                <p>Description: <textarea name='Description'></textarea></p>
                <p>Url: <input type='text' name='Url'></p>
                <input type='submit' value='Submit'>
            </form>";
}
//Display items in Closet
echo "<h2>Storage</h2>";
$query = "SELECT * FROM items WHERE Status = '0' ORDER BY Type";
$result = mysqli_query($con, $query);

$current_type = null;
while ($row = mysqli_fetch_assoc($result)) {
    //Add Type headers
    if($row['Type'] != $current_type) {
        echo "<h3>" . $row['Type'] . "</h3>";
        $current_type = $row['Type'];
    }
    echo "<div>" . $row['Name'] . "</div>";
    echo "<div>" . $row['Pounds'] . " lb</div>";
    echo "<div>" . $row['Ounces'] . " oz</div>";

            //Delete Item from Database
    echo    "<form action='index.php' method='post'>
                <input type='hidden' name='id' value='$row[ID]'>
                <input type='hidden' name='delete' value='yes'>
                <input type='submit' value='Discard Item'>
            </form>";

            //Move Item from Closet to Pack
    echo    "<form action='index.php' method='post'>
                <input type='hidden' name='id' value='$row[ID]'>
                <input type='hidden' name='toPack' value='yes'>
                <input type='submit' value='Move to Pack'>
            </form>";
            //Modify Item
    echo    "<form action='index.php' method='post'>
                <input type='hidden' name='Modify' value='yes'>
                <input type='hidden' name='id' value='$row[ID]'>
                <input type='hidden' name='ItemName' value='$row[Name]'>
                <input type='hidden' name='Type' value='$row[Type]'>
                <input type='hidden' name='Pounds' value='$row[Pounds]'>
                <input type='hidden' name='Ounces' value='$row[Ounces]'>
                <input type='hidden' name='Description' value='$row[Description]'>
                <input type='hidden' name='Url' value='$row[Url]'>
                <input type='Submit' value='Modify'>
            </form>";
}

//Display items in Pack
echo "<h2>Pack</h2>";
$query = "SELECT * FROM items WHERE Status='1' ORDER BY Type";
$result = mysqli_query($con, $query);
if(!$result) {
    echo mysqli_error($con);
}
$current_type = null;
while ($row = mysqli_fetch_assoc($result)) {
    //Add Type headers
    if($row['Type'] != $current_type) {
        echo "<h3>" . $row['Type'] . "</h3>";
        $current_type = $row['Type'];
    }
    echo "<div>" . $row['Name'] . "</div>";
    echo "<div class='packPounds'>" . $row['Pounds'] . "</div><span> lb</span>";
    echo "<div class='packOunces'>" . $row['Ounces'] . "</div><span> oz</span>";
    echo    "<form action='index.php' method='post'>
                <input type='hidden' name='id' value='$row[ID]'>
                <input type='hidden' name='delete' value='yes'>
                <input type='submit' value='Discard Item'>
            </form>";

    //Move Item from Pack to Closet
    echo    "<form action='index.php' method='post'>
                <input type='hidden' name='toCloset' value='yes'>
                <input type='hidden' name='id' value='$row[ID]'>
                <input type='submit' value='Move to Closet'>
            </form>";
}

//Close the Database Connection
mysqli_close($con);

//Functions Go Here
function get_post($var) {
    return mysql_real_escape_string($_POST[$var]);
}
?>
<script src="theme.js"></script>
<div id="packTotal"></div>
</body>
</html>

0 个答案:

没有答案