如何使用PHP和MySQL创建编辑表单以更新HTML表单中的记录

如何使用PHP和MySQL创建编辑表单以更新HTML表单中的记录,php,html,mysql,css,Php,Html,Mysql,Css,我有一个脚本,它使用HTML中的值更新MySQL表,并由PHP处理。 当我单击表上的编辑链接时,它会将我重定向到编辑页面,其中显示从MySQL数据库获取的记录,但不会更新记录 这是我的链接代码: echo "<td><a href=\"cityproc.php?accode=$row[accode]\"><img src='images/edit.png'></a></td>"; echo”“; 以下是我的编辑页面代码: <?

我有一个脚本,它使用HTML中的值更新MySQL表,并由PHP处理。 当我单击表上的编辑链接时,它会将我重定向到编辑页面,其中显示从MySQL数据库获取的记录,但不会更新记录

这是我的链接代码:

echo "<td><a href=\"cityproc.php?accode=$row[accode]\"><img src='images/edit.png'></a></td>";
echo”“;
以下是我的编辑页面代码:

<?php
session_start();
if (!isset($_SESSION["username"])) {
    header("Location: unauthorize_access.php");
}
mysql_connect("localhost", "root", '')or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());

$accode = mysql_real_escape_string($_REQUEST['accode']); // is used for both $_GET/$_POST variables

if(isset($_POST['submit']))
{

          $city = mysql_real_escape_string($_POST['city']);
          $result = mysql_query("UPDATE `city` SET `name`='$city' WHERE accode='$accode'") or die(mysql_error());

          echo "<b>Thank you! Record UPDATED Successfully!<br>You'll be redirected to Home Page after (1) Seconds";
          echo "<meta http-equiv=Refresh content=1;url=table.php>";
}
elseif($accode)
{

        $result = mysql_query("SELECT * FROM city WHERE accode='$accode' ");
        $myrow = mysql_fetch_assoc($result);

                $code = $myrow["code"];
                $city = $myrow["name"];
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link rel="stylesheet" type="text/css" href="form2/view.css" media="all">
<script type="text/javascript" src="form2/view.js"></script>
<script type="text/javascript" src="form2/calendar.js"></script>
</head>
<body id="main_body" >

    <img id="top" src="form2/top.png" alt="" />
    <div id="form_container">

        <h1><a>City</a></h1>
        <form id="city" class="appnitro" enctype="multipart/form-data" method="post" action="cityproc.php">
                    <div class="form_description">
        <h2>City</h2>
                <table border="0" width="100%">
                    <tr>
                        <td><?php echo $accode; ?></td>
                    </tr>
            </table>

        </div>
                    <table border ="0px" width="100%">
                        <input type="hidden" value="<? echo $myrow['accode']?>" name="accode"></input> 
                        <tr>
                            <td><label class="description" for="element_1">Code</label></td><td><input  name="code"  type="text" maxlength="6"  Placeholder="Please enter a code" value="<?php echo $code; ?>" disabled="disabled" /></td>
                        </tr>
                        <tr>
                            <td><label class="description" for="element_1">Name</label></td><td><input  name="city" size="40" type="text" maxlength="40" Placeholder="Please enter a name" value="<?php echo $city; ?>"/></td>

                        </tr>
                        <tr>
                            <td></td><td colspan="2" align="center"><input type="submit" name="submit" value="Save" /></td>
                        </tr>
                    </table>
                </form>
                </body>
    </html>
<?php
}
?>
试试这个。。。。
$link=mysql\u connect(“localhost”、“root”或“”)或die(mysql\u error());
mysql_选择_db(“webapp”,$link)或die(mysql_error())

mysql_query(“更新
city
SET
name
='$city'其中accode='$accode'”,$link)


回显更新查询并在phpmyadmin中手动运行它。

由于您已在同一页面中编写了所有代码,因此不需要在此处编写表单操作。在提交时,您的页面将自动刷新。试着这样做一次,然后试着通过打印
print\r($\u POST)
来调试代码,看看您是否得到了数据

此外,您编写的查询也不正确。您必须将其写为:

$result = mysql_query("UPDATE `city` SET `name`='" . $city . "' WHERE accode='" . $accode . "') or die(mysql_error());

在cityproc.PHP中编写PHP代码

另一个文件中的html代码是cityproc.html


然后检查并告知错误是什么

运行该查询时收到的错误是什么?使用
print\r($\u POST);死亡(“以提交形式”)if
子句中编写>并检查数据。没有任何错误,只是在单击提交按钮后,更新查询似乎没有更新我数据库中的记录。您需要在文件
cityproc.php
中编写更新代码,这是
表单的操作,或者更改表单的操作并在同一页面上提交表单运行的表单本身就是操作表单,并且它还显示回显消息“谢谢!记录更新成功!
您将在(1)秒后重定向到主页”;但是它仍然没有更新记录]首先回显更新查询,并在phpmyadmin中手动运行查询。是的,我正在表单上获取数据,但在单击“提交”按钮后,它没有更新记录。但是,您是否如我上面所述更改了查询行?你需要像上面那样进行更改,然后才能正确地获得更新。
        $city = mysql_real_escape_string($_POST['city']);
        if(!empty($city)) {
            try {
                $result = mysql_query("UPDATE `city` SET `name`= '$city' WHERE accode='$accode'");
            } catch (Exception $e) {
                var_dump($e->getMessage()); // see what's the error.
            }

            if ($result) {
                echo $result;
            } else {
                echo $result;
            }
        }