数据库问题:无法通过PHP和MYSQL更新
目前正在处理一个项目,无法解决此问题。URL中的$\u POST ID工作正常,但我无法使用该ID获取行。请帮助 花了很多时间浏览代码并在不同的区域发表评论,以获得数据库问题:无法通过PHP和MYSQL更新,php,html,mysql,Php,Html,Mysql,目前正在处理一个项目,无法解决此问题。URL中的$\u POST ID工作正常,但我无法使用该ID获取行。请帮助 花了很多时间浏览代码并在不同的区域发表评论,以获得的效果 数据库连接工作-为了隐私,我刚刚删除了$con <?php $con = mysqli_connect(blablabla; if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error();
的效果
数据库连接工作-为了隐私,我刚刚删除了$con
<?php
$con = mysqli_connect(blablabla;
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " .
mysqli_connect_error();
if (isset($_POST['update'])) {
$id = $_POST['id'];
$Catname = $_POST['Catname'];
$Catdescription = $_POST['Catdescription'];
$Catpicture = base64_encode(file_get_contents($_FILES['Catpicture']
['tmp_name']));
$result = mysqli_query($mysqli, "UPDATE CatadoptionDB SET Catname='$Catname',Catdescription='$Catdescription',Catpicture='$Catpicture' WHERE id=$id");
}
}
?>
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM CatadoptionDB WHERE id=$id");
while ($res = mysqli_fetch_array($result)) {
$Catname = $res['Catname'];
$Catdescription = $res['Catdescription'];
$Catpicture = $res['Catpicture'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<form name="form1" method="post" action="" enctype="multipart/form-data">
<table border="0">
<tr>
<td>Catname</td>
<td>
<input type="text" name="Catname" value='<?php echo
$Catname; ?>'>
</td>
</tr>
<tr>
<td>Catdescription</td>
<td><textarea name="Catdescription" value='<?php echo
$Catdescription; ?>'>
</textarea>
</td>
</tr>
<tr>
<td>Catpicture</td>
<td><input type="file" name="Catpicture" value='<?php echo
$Catpicture; ?>'>
</td>
</tr>
<tr>
<td><input type="hidden" name="id" value='<?php echo
$_GET['id']; ?>'>
</td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
首先,表单方法设置为'post',因此您应该通过$\u post['id']获取$id值
其次,根据您的代码,更新表只会在db connect error上运行,所以它永远不会更新
也许你可以试着做如下的小改变
<?php
$con = mysqli_connect();
if (mysqli_connect_errno()) die("Failed to connect to MySQL: " . mysqli_connect_error() );
$id = isset( $_GET['id']) ? $_GET['id'] : $_POST['id'];
if (isset($_POST['update'])) {
$id = $_POST['id'];
$Catname = $_POST['Catname'];
$Catdescription = $_POST['Catdescription'];
$Catpicture = base64_encode(file_get_contents($_FILES['Catpicture']
['tmp_name']));
$result = mysqli_query($mysqli, "UPDATE KatadoptionDB SET Catname='$Catname',Catdescription='$Catdescription',Catpicture='$Catpicture' WHERE id=$id");
}
?>
<?php
$result = mysqli_query($mysqli, "SELECT * FROM CatadoptionDB WHERE id=$id");
while ($res = mysqli_fetch_array($result)) {
$Catname = $res['Catname'];
$Catdescription = $res['Catdescription'];
$Catpicture = $res['Catpicture'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<form name="form1" method="post" action="" enctype="multipart/form-data">
<table border="0">
<tr>
<td>Catname</td>
<td>
<input type="text" name="Catname" value='<?php echo
$Catname; ?>'>
</td>
</tr>
<tr>
<td>Catdescription</td>
<td><textarea name="Catdescription" value='<?php echo
$Catdescription; ?>'>
</textarea>
</td>
</tr>
<tr>
<td>Catpicture</td>
<td><input type="file" name="Catpicture" value='<?php echo
$Catpicture; ?>'>
</td>
</tr>
<tr>
<td><input type="hidden" name="id" value='<?php echo $id; ?>'>
</td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
如果要在textarea输入中显示$Catdescription值,请先更改,然后重试
<td><textarea name="Catdescription" value='<?php echo
$Catdescription; ?>'>
</textarea>
</td>
在这两个查询中,您都通过了$mysqli
而不是$con
。感谢您的帮助,但还不能完全正常工作。将$mysqli
更改为$con
现在显示输入值中的$catname
。但不显示$Catdescription
,也不更新数据库。
<td><textarea name="Catdescription" ><?php echo
$Catdescription; ?>
</textarea>
</td>