0

As title say, the button "eliminaRecensioneBTN" inside td don't work. I tryed to put the button outsite the tag and it worked, but if I put that button outside the td tag it work; I need that inside.

PHP code:

    <?php
    session_start();

    if (!(isset($_SESSION['autorizzato']) && $_SESSION['autorizzato'] == true)) {
        header("Location: login.php");
    }
    $nomepagina = 'sezioni';

    $conn = mysqli_connect('localhost', 'root', '', 'gestione');
    if (!$conn) {
        die("Connessione non riuscita: ".mysqli_connect_error());
    }

    function prendiRecensioni($conn){
        $sql = "SELECT * FROM recensioni";
        $result = $conn->query($sql);
        while($row = $result->fetch_assoc()){
            echo "<form method='POST' action='".eliminaRecensione($conn)."'>";
                echo "<tr>";
                    echo "<td>".$row['id']."</td>";
                    echo "<td>".$row['nomevisualizzato']."</td>";
                    echo "<td>".$row['pubblicazione']."</td>";
                    echo "<td>".mb_strimwidth($row['descrizione'], 0, 20, "...")."</td>";
                    echo "<input type='hidden' name='id' value='".$row['id']."' />";
                    echo "<td class='text-right'>";
                        echo "<button type='submit' name='eliminaRecensioneBTN' class='btn btn-danger btn-xs'><i class='fa fa-trash'></i>&nbsp;&nbsp;Elimina</button>";
                    echo "</td>";
                echo "</tr>";
            echo "</form>";
        }
    }

    function eliminaRecensione($conn){
        if(isset($_POST['eliminaRecensioneBTN'])){
            $id = $_POST['id'];
            $sql = "DELETE FROM recensioni WHERE id='$id'";
            $result = $conn->query($sql);
            echo "<meta http-equiv='refresh' content='0'>";
        }
    }
?>

I dont' know how to fix, so how can I fix it?

1 Answers1

0

Remove the action attribute from the <form> tag so the form will POST to the same URL it's on.

In the top of the file, check if the form is posted and delete the post using the SQL query.

After that, get the results and display the forms using HTML.

Don't forget to put a <table> tag around your <tr> and <td> tags!

Put the hidden input at the bottom of the <form> tag, outside the <table>. It isn't going to be displayed anyway!

<?php
$conn = mysqli_connect( 'localhost', 'root', '', 'gestione' );
if ( ! $conn ) {
    die( "Connessione non riuscita: " . mysqli_connect_error() );
}

if ( isset( $_POST['eliminaRecensioneBTN'] ) && isset( $_POST['id'] ) ) {
    $conn->query( "DELETE FROM recensioni WHERE id='$_POST['id']'" );
    echo '<meta http-equiv="refresh" content="0">';
}

$result = $conn->query( 'SELECT * FROM recensioni' );
?>

<?php while ( $row = $result->fetch_assoc() ): ?>
    <form method="POST">
        <table>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['nomevisualizzato']; ?></td>
                <td><?php echo $row['pubblicazione']; ?></td>
                <td><?php echo mb_strimwidth( $row['descrizione'], 0, 20, '...' ); ?></td>
                <td class="text-right">
                    <button type="submit" name="eliminaRecensioneBTN" class="btn btn-danger btn-xs">
                        <i class="fa fa-trash"></i>&nbsp;&nbsp;Elimina
                    </button>
                </td>
            </tr>
        </table>
        <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
    </form>
<?php endwhile; ?>
jrswgtr
  • 2,287
  • 8
  • 23
  • 49