如何创建编辑表单以使用 PHP 和 MySQL 从 HTML 表单更新记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14393592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a edit form to update records from the HTML forms using PHP and MySQL
提问by john
I have a script that updates MySQL tables with the values from an HTML and processed by PHP. When I click on the edit link which is on the table it redirects me to the edit page, where it shows the records fetched from the MySQL database - but it does not update the record.
我有一个脚本,它使用 HTML 中的值更新 MySQL 表并由 PHP 处理。当我点击表上的编辑链接时,它会将我重定向到编辑页面,在那里它显示从 MySQL 数据库获取的记录 - 但它不会更新记录。
Here is my link code:
这是我的链接代码:
echo "<td><a href=\"cityproc.php?accode=$row[accode]\"><img src='images/edit.png'></a></td>";
Here is my edit page code:
这是我的编辑页面代码:
<?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
}
?>
采纳答案by Philip F
$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;
}
}
回答by Anshita Patel
As you have written all the code in the same page it is not required to write the form action here. On submit your page will be automatically refreshed. Try doing this once and try to debug the code by printing print_r($_POST)and see whether you get your data or not.
由于您已在同一页面中编写了所有代码,因此无需在此处编写表单操作。提交时,您的页面将自动刷新。尝试执行一次并尝试通过打印来调试代码print_r($_POST),看看是否获得了数据。
Also the query you have written is not correct. You have to write it as :
此外,您编写的查询不正确。你必须把它写成:
$result = mysql_query("UPDATE `city` SET `name`='" . $city . "' WHERE accode='" . $accode . "') or die(mysql_error());
回答by ????? ???
Write your PHP code in cityproc.php
在 cityproc.php 中编写您的 PHP 代码
and html code in another file say cityproc.html
和另一个文件中的 html 代码说 cityproc.html
then check and tell what is the error
然后检查并告诉错误是什么
回答by Kaspar Mary
Try this.... $link = mysql_connect("localhost", "root", '')or die(mysql_error()); mysql_select_db("webapp",$link) or die(mysql_error());
试试这个.... $link = mysql_connect("localhost", "root", '')or die(mysql_error()); mysql_select_db("webapp",$link) 或 die(mysql_error());
Or
或者
mysql_query("UPDATE citySET name='$city' WHERE accode='$accode'", $link)
mysql_query("UPDATE citySET name='$city' WHERE accode='$accode'", $link)
Echo the update query and run it manually in phpmyadmin.
回显更新查询并在 phpmyadmin 中手动运行它。

