在MySQL和PHP中编辑表单

时间:2012-03-17 01:42:06

标签: php mysql forms

我正在开展一个项目。我需要做的是基本上将一些信息输入到表单中,将该表单保存到数据库中,显示数据,然后才能编辑数据。到目前为止,除了编辑数据外,我能够做所有事情。我已经尝试使用$ _GET来获取我需要编辑的特定“bug”的ID,我能够做到这一点,并获取所有信息,但我不知道如何在我的数据库中编辑该特定ID 。这是我的处理程序:http://pastebin.com/mR6QWpJ7和我的表单:

<form action="week10handle.php" method="POST">
        <fieldset width="300px">
            <legend width="300px"><b>Add a bug report</b></legend>
                Product Name:<br/><input type="text" name="product_name"><br/>
                Product Version: <br/><input type="text" name="product_version"><br/>
                Hardware Type: <br/><input type="text" name="hardware"><br/>
                Operating System: <br/><input type="text" name="os"><br/>
                Frequency: <br/><input type="text" name="frequency"><br/>
                Proposed Solutions: <br/><textarea name="solutions"></textarea><br/>
                <input type="submit" value="Submit">
        </fieldset>
</form>

到目前为止,我在编辑表单页面中获取获取数据的位置,但截至目前,我不知道如何编辑数据库中的特定ID。

    $getbug = htmlspecialchars($_GET["bugid"]);


    if (!empty($getbug)){
        $getbuginfo = mysql_query("SELECT * FROM `bugs` WHERE `id`= '$getbug'");
        if ($getbuginfo = mysql_fetch_assoc($getbuginfo)){
            $edit_product_name = $getbuginfo['product_name'];
            $edit_prod_version = $getbuginfo['product_version'];
            $edit_hardware = $getbuginfo['hardware_type'];
            $edit_os = $getbuginfo['os'];
            $edit_frequency = $getbuginfo['frequency'];
            $edit_solutions = $getbuginfo['solutions'];
            ?>
<form action="week10handle.php" method="POST">
        <fieldset width="300px">
            <legend width="300px"><b>Edit bug <?php echo $getbug;?></b></legend>
                Product Name:<br/><input type="edit" name="product_name" value="<?php echo $edit_product_name;?>"><br/>
                Product Version: <br/><input type="edit" name="product_version" value="<?php echo $edit_prod_version;?>"><br/>
                Hardware Type: <br/><input type="edit" name="hardware" value="<?php echo $edit_hardware;?>"><br/>
                Operating System: <br/><input type="edit" name="os"value="<?php echo $edit_os;?>"><br/>
                Frequency: <br/><input type="edit" name="frequency"value="<?php echo $edit_frequency;?>"><br/>
                Proposed Solutions: <br/><textarea name="solutions"><?php echo $edit_product_name;?></textarea><br/>
                <input type="submit" value="Submit">
        </fieldset>
</form>
编辑:这是我的更新PHP代码,但它仍然无法正常工作,当我提交表单时,它会刷新页面,但它不会更新数据库:

<?php
if (mysql_connect('localhost','root','') && mysql_select_db('bug_reports')){

    $errors = array();

    if (isset($_POST['product_name'], $_POST['product_version'],$_POST['hardware'],$_POST['os'],$_POST['frequency'], $_POST['solutions'])){
        $product_name = mysql_real_escape_string(htmlentities($_POST['product_name']));
        $product_version = mysql_real_escape_string(htmlentities($_POST['product_version']));
        $hardware = mysql_real_escape_string(htmlentities($_POST['hardware']));
        $os = mysql_real_escape_string(htmlentities($_POST['os']));
        $frequency = mysql_real_escape_string(htmlentities($_POST['frequency']));
        $solutions = mysql_real_escape_string(htmlentities($_POST['solutions']));
        $getbug = mysql_real_escape_string(htmlentities($_POST['bugid']));
        if (empty($product_name) || empty($product_version) || empty($hardware) || empty($os) || empty($frequency) || empty($solutions)){
            $errors[] = 'All fields are required.';
        }

        if (!is_numeric($product_version) || !is_numeric($frequency)){
            $errors[] = 'Product version and frequency must both be numbers';
        }

        if (empty($errors)){
            $update = "UPDATE `bugs` SET `product_name` = '$product_name', `product_version = '$product_version', `hardware_type = '$hardware', `os` = '$os', `frequency` = '$frequency', `solutions` = '$solutions' WHERE `id` = $getbug";
            if ($update = mysql_query($update)){
                header('Location: week10handle.php');
            } else{
                $errors[] = 'Something went wrong, please try again.';
            }
        } else{
            foreach($errors as $error){
                echo '<p><strong>'.$error.'</strong></p>';
            }
        }

    }else{
    $getbug = htmlspecialchars($_GET["bugid"]);
    }



    if (!empty($getbug)){
        $getbuginfo = mysql_query("SELECT * FROM `bugs` WHERE `id`= '$getbug'");
        if ($getbuginfo = mysql_fetch_assoc($getbuginfo)){
            $bugid = $getbuginfo['id'];
            $edit_product_name = $getbuginfo['product_name'];
            $edit_prod_version = $getbuginfo['product_version'];
            $edit_hardware = $getbuginfo['hardware_type'];
            $edit_os = $getbuginfo['os'];
            $edit_frequency = $getbuginfo['frequency'];
            $edit_solutions = $getbuginfo['solutions'];
            ?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <fieldset width="300px">
            <legend width="300px"><b>Edit bug <?php echo $getbug;?></b></legend>
                Product Name:<br/><input type="edit" name="product_name" value="<?php echo $edit_product_name;?>"><br/>
                Product Version: <br/><input type="edit" name="product_version" value="<?php echo $edit_prod_version;?>"><br/>
                Hardware Type: <br/><input type="edit" name="hardware" value="<?php echo $edit_hardware;?>"><br/>
                Operating System: <br/><input type="edit" name="os"value="<?php echo $edit_os;?>"><br/>
                Frequency: <br/><input type="edit" name="frequency"value="<?php echo $edit_frequency;?>"><br/>
                Proposed Solutions: <br/><textarea name="solutions"><?php echo $edit_product_name;?></textarea><br/>
                <input type="hidden" name="bugid" value="<?php echo $bugid;?>" >
                <input type="submit" value="Update">
        </fieldset>
    </form>
<?
        }else{
            echo "something went wrong";
        }
    }else{
        echo "No bug found.";
    }

}else
    echo 'Could not connect at this time.';

?>

2 个答案:

答案 0 :(得分:1)

检测更新的典型方法是检查id的值,而不是插入。因此,在编辑表单中添加一个隐藏字段以将id传递给处理程序,然后在处理程序中,您可以根据id字段的存在来决定是将其作为插入还是更新进行处理。

if (isset($_GET['id']) {

    // do update
    $sql = 'UPDATE `bugs` SET ... WHERE id = ' . intval($_GET['id']);

} else {

    // do insert
    $sql = 'INSERT INTO `bugs` VALUES ....';

}

答案 1 :(得分:-1)

UPDATE `bugs` SET `product_name` = '...', `product_version` = '...', ... WHERE `id` = $bugid;

其中“...”将替换为每列的新$ _POST-ed值