Php 编辑记录(应该很容易)
我在Stackoverflow中到处都找过,我在Google上搜索了16.493个站点,但没有找到php中最基本内容的答案(编辑记录) 我成功地编写了最复杂的代码——但这就像癌症一样,也会帮助其他人 我必须修改文件edit.php和update.php php工作,它从记录中检索数据 这里是edit.phpPhp 编辑记录(应该很容易),php,edit,Php,Edit,我在Stackoverflow中到处都找过,我在Google上搜索了16.493个站点,但没有找到php中最基本内容的答案(编辑记录) 我成功地编写了最复杂的代码——但这就像癌症一样,也会帮助其他人 我必须修改文件edit.php和update.php php工作,它从记录中检索数据 这里是edit.php <?php mysql_connect('localhost', 'user', 'pass') or die(mysql_error()); mysql_select_db("db"
<?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
$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();
?>
我认为简单的答案是,您从未将“id”张贴到update.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>
这将把它放在$\u GET数组中,在$\u请求数组中也会看到它
最后,您的代码存在一些主要问题:
- 首先也是最重要的一点是,它受制于SQL注入!你必须逃跑
在将变量传递到MySQL查询之前,请先删除这些变量
- 第二。正如IDiffer所指出的,您似乎在fs字段中回显了错误的值(您将其设置为等于texti字段)
- 第三,为什么会有这个循环
if(mysql_num_rows($query)>=1){
while($row=mysql\u fetch\u array($query)){
$fs=$row['fs'];
$texti=$row['texti'];
}
如果您是通过ID获取的,您不应该有重复项。请确保ID是主键,并且没有理由检查多行。什么不起作用?有错误吗?您是否尝试过echo$ID、$fs、$texti;
?尝试回显您的查询并粘贴到mysql客户端,或者检查mysql\u error()更新后。另外,您的代码易受SQL注入攻击。请告诉我们错误,并始终记住使用mysql\u real\u escape\u string()。还有
<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>