Currently working on a project and cannot get through this problem. The $_POST ID in URL works fine, but I cannot fetch the row using the ID. Please help!

Spent a lot of time going through the code and outcomment different areas to get <?php echo $result to work, but it does not echo the result since the result does not exist (?Why?). <?php php echo $ID ?> works though.

Database connection works - I just removed the $con for privacy

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

I expect it to output the string of the variable in each of the values, and I expect it to update the table, which it doesn't. It just doesn't echo anything. Same with $result.

2 Answers

1
tiebob On

first, your form method is setted 'post', so you should get $id value via $_POST['id'] second, according you code, UPDATE TABLE will just run on db connect error, so it will never update.

Maybe you can try to make a small change like the following.

<?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>
0
Dipali Sakle Systematix On

if you want to display $Catdescription value in textarea input then firstly change and then try

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

to

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