简体   繁体   中英

How do I change state value on MySQL with an a href?

I can't change the state of a value with a href. I have tried in all ways. Here is my code

 <a href="giallo.php?id=' . $row['id'] . '">Giallo</a>  

giallo.php=

<?php
                            
// Create connection
$conn = new mysqli('localhost','root','','agenda');
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
 
$id = $_GET['id']; 

$qry = mysqli_query($db,"select * from note where id='$id'"); // select query

// when click on Update button
if(isset($_POST['update'])) {
    $colore=1;
    
    $edit = mysqli_query($db,"update note set colore='$colore' where id='$id'");
    
    if($edit) {
        mysqli_close($db); // Close connection
        header("location:udienze.php"); // redirects to all records page
        exit;
    } else {
        echo mysqli_error();
    }   
}

if (mysqli_query($conn, $sql)) {
    echo "<script>
    alert('Nota inserita correttamente');
    window.location.href='add-udienze.php';
    </script>";
} else {
    echo "<script>
    alert('Errore');
    window.location.href='add-udienze.php';
    </script>";
}
    
mysqli_close($conn);
?>

What is wrong with my code? There are probably cleaner ways to do it. I have tried all ways that I know.

I think the problem is that you are calling the giallo.php script with multiple kind of params (POST and GET).

So when you click on the link, the "href" attribute redirects to giallo.php, but nothing happen because it miss the $_POST['update'] action.

Probably the solution fit your case can be edit the href attribute, adding a GET parameter for "update", like:

<a href="giallo.php?update=1&id=' . $row['id'] . '">Giallo</a>  

And then edit the giallo.php file and consider the new $_GET["update"] and not the POST one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM