繁体   English   中英

在MySQL和PHP中编辑表单

[英]Editing a form in MySQL and PHP

我正在做一个项目。 我要做的基本上是在表单中输入一些信息,将该表单保存到数据库中,显示数据,然后能够编辑数据。 到目前为止,除了编辑数据之外,我已经可以执行所有操作。 我尝试使用$ _GET来获取我需要编辑的特定“错误”的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.';

?>

与插入操作不同,检测更新的典型方法是检查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 ....';

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

对于每列,“ ...”将被新的$ _POST-ed值替换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM