编辑记录(应该很容易)


Edit record (should be so easy)

我在 Stackoverflow 中到处找过,我在 Google 上搜索了 16.493 个网站,但没有回答 php 中最基本的问题(编辑记录(

我已经设法编写了最复杂的东西 - 但这就像癌症,也会帮助其他人。

我必须文件 - 编辑.php - 和更新.php

编辑.php工作并从记录中检索数据

这是编辑.php

<?php
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$UID = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM cloudbig WHERE id = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
    while($row = mysql_fetch_array($query)) {
        $fs = $row['fs'];
        $texti = $row['texti'];
    }
?>
<form name="form1" method="post" action="update.php">
<input type="text" name="fs" value="<?php echo $texti ?>" size="60">
<textarea rows="8" name="texti"  id="userName" cols="60"><?php echo $texti ?></textarea>
<input type="submit" name="save" value="submit" />
</form>
<?php
}
?>

这是更新.php

<?php
$id = $_REQUEST["id"];
$fs = $_POST["fs"];
$texti = $_POST["texti"];
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
echo "MySQL Connection Established! <br>";
mysql_select_db("db") or die(mysql_error());
echo "Database Found! <br>";
$query = "UPDATE cloudbig SET fs = '$fs', texti = '$texti' WHERE id = '$id'";
$res = mysql_query($query);
if ($res)
  echo "<p>Record Updated<p>";
else
  echo "Problem updating record. MySQL Error: " . mysql_error();
?>

我已经用php做了一个完整的新闻/在线杂志网站,但简单的编辑.php功能是一个问题

我认为简短的回答是你永远不会将"id"发布到更新.php脚本中。表单需要如下所示:

<form name="form1" method="post" action="update.php">
   <input type="hidden" name="id" value="<?php echo $UID  ?>">
   <input type="text" name="fs" value="<?php echo $fs; ?>" size="60">
  <textarea rows="8" name="texti"  id="userName" cols="60"><?php echo $texti ?></textarea>
   <input type="submit" name="save" value="submit" />
</form>

这会将id发送到 POST 数组中,$id = $_REQUEST["id"];

您也可以通过修改表单_GET发送action来实现此目的:

<form name="form1" method="post" action="update.php?id=<?php echo $UID  ?>">
   <input type="text" name="fs" value="<?php echo $fs; ?>" size="60">
   <textarea rows="8" name="texti"  id="userName" cols="60"><?php echo $texti ?></textarea>
    <input type="submit" name="save" value="submit" />
 </form>

这会将其放在 $_GET 数组中,它也将在 $_REQUEST 数组中看到。

最后,您的代码存在一些主要问题:

  • 首先,它受制于SQL注入!你必须逃脱在将变量传递到 MySQL 查询之前,您的变量。

  • 第二。正如 iDifferent 所指出的,您似乎将错误的值回显到 fs 字段中(您将其设置为等于 texti 字段(

  • 第三,你为什么会有这个循环?

    if(mysql_num_rows($query(>=1({ 而($row = mysql_fetch_array($query(( { $fs = $row['fs']; $texti = $row['texti']; }

如果您按 ID 提取,则不应有重复项。确保 ID 是主键,并且没有理由检查多行。

相关文章: