数据库问题:无法通过PHP和MYSQL更新

时间:2019-04-30 10:26:11

标签: php html mysql

当前正在处理项目,无法解决此问题。 URL中的$ _POST ID可以正常工作,但是我无法使用该ID来获取该行。请帮忙!

花了很多时间遍历代码并推荐不同的区域来使<?php echo $result正常工作,但是由于结果不存在,所以它没有回显结果(为什么?)。 <?php php echo $ID ?>可以工作。

数据库连接有效-我只是出于隐私目的删除了$ 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>

我希望它在每个值中输出变量的字符串,并且我希望它更新表,但不是。它只是不回声。与$ result相同。

2 个答案:

答案 0 :(得分:1)

首先,将表单方法设置为“ post”,因此您应该通过$ _POST ['id']获取$ id值 其次,根据您的代码,UPDATE TABLE将仅在db connect错误上运行,因此它将永远不会更新。

也许您可以尝试进行如下更改。

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

答案 1 :(得分:0)

如果要在文本区域输入中显示$ Catdescription值,请先进行更改,然后尝试

 <td><textarea name="Catdescription" value='<?php echo
                $Catdescription; ?>'>
            </textarea>
            </td>

  <td><textarea name="Catdescription" ><?php echo
                $Catdescription; ?>
            </textarea>
            </td>