gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:12:29 25 4
gpt4 key购买 nike

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

这是我的链接代码:

echo "<td><a href=\"cityproc.php?accode=$row[accode]\"><img src='images/edit.png'></a></td>";

这是我的编辑页面代码:

<?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
}
?>

最佳答案

        $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;
}
}

关于php - 如何使用 PHP 和 MySQL 创建编辑表单以更新来自 HTML 表单的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14393592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com