php - 编辑mysql数据库中的一行

标签 php mysql

我有一个用于收集测试数据的数据库。

它在新行上显示每条记录,末尾有一个编辑和删除链接,还有一个添加新记录链接。

除编辑部分外,一切正常。我看不出哪里出错了?

当我点击编辑链接时,它会显示带有字段等的表格布局,但它只传递行 ID,并在日期字段中显示它。如果我在字段中输入并提交,它将发生变化。它只是不会传递数据。

这是我的编辑页面和编辑脚本。

   <?php
function valid($date,$error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Records</title>
</head>
<body>
<?php

if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>

<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $date; ?>"/>

<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Edit Records </font></b></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Date</font></b></td>
<td><label>
<input type="text" name="date" value="<?php echo $date; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Ammonia</em></font></b></td>
<td><label>
<input type="text" name="amm" value="<?php echo $amm; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Nitrate</font></b></td>
<td><label>
<input type="text" name="nat" value="<?php echo $nat; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Nitrite</font></b></td>
<td><label>
<input type="text" name="nit" value="<?php echo $nit; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>pH</font></b></td>
<td><label>
<input type="text" name="ph" value="<?php echo $ph; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Alkalinity</font></b></td>
<td><label>
<input type="text" name="alk" value="<?php echo $alk; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>SG</font></b></td>
<td><label>
<input type="text" name="sg" value="<?php echo $sg; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Temperature</font></b></td>
<td><label>
<input type="text" name="temp" value="<?php echo $temp; ?>" />
</label></td>
</tr>

<tr align="Right">
<td colspan="2"><label>
<input type="submit" name="submit" value="Edit Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

include('config.php');

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

if (is_numeric($_POST['id']))
{

$id = $_POST['id'];
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$amm = mysql_real_escape_string(htmlspecialchars($_POST['amm']));
$nat = mysql_real_escape_string(htmlspecialchars($_POST['nat']));
$nit = mysql_real_escape_string(htmlspecialchars($_POST['nit']));
$ph = mysql_real_escape_string(htmlspecialchars($_POST['ph']));
$alk = mysql_real_escape_string(htmlspecialchars($_POST['alk']));
$sg = mysql_real_escape_string(htmlspecialchars($_POST['sg']));
$temp = mysql_real_escape_string(htmlspecialchars($_POST['temp']));


if ($date == '')
{

$error = 'ERROR: Please fill in all required fields!';


valid($date, $error);
}
else
{

mysql_query("UPDATE employee SET date='$date', amm='$amm', nat='$nat', nit='$nit', ph='$ph', alk='$alk', sg='$sg', temp='$temp'")
or die(mysql_error());

header("Location: view.php");
}
}
else
{

echo 'Error!';
}
}
else

{

if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM employee WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);

if($row)
{

$date = $row['date'];
$amm = $row['amm'];
$nat = $row['nat'];
$nit = $row['nit'];
$ph = $row['ph'];
$alk = $row['alk'];
$sg = $row['sg'];
$temp = $row['temp'];

valid($id,'');
}
else
{
echo "No results!";
}
}
else

{
echo 'Error!';
}
}
?>

最佳答案

valid 函数负责呈现您的表单。但是您只将 $date 传递给此函数,因此这是它唯一可以填写的内容。您还必须传递其他值!

关于php - 编辑mysql数据库中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857856/

相关文章:

javascript - 将 MySQL 数据库中的 UTC 日期时间转换为客户端时区,无需任何库

sql - MySQL查询帮助(涉及连接?)

php - PHP socket_accept在非阻塞模式下引发警告

php - 使用键值中带有句点的数组

php - laravel MySQL 查询优化

mysql - 经典 ASP 在打开 MySQL 时生成 500 错误

php - 在数据库中的字符串中搜索 '_',然后向其中输入一个变量?

每 10 分钟执行一次 php cron 作业

php - 未找到 Laravel excel 类 'ZipArchive'

mysql - if 条件下 mysql 中的错误#1064