Helps make a condition in php

Asked

Viewed 301 times

5

I have my php code here so you can delete the client, but only delete when you write the client name (which is in html). but I don’t know how to make the condition if someone writes a name of a client that isn’t in the database. And also I do not understand when I put a name to it same as the deleted account message :/ , does not make the LSE

   <script language="javascript">
<!--
function myFunction(a) {
    alert(a);
}

</script>

<?php

ini_set ('default_charset','utf-8');

$link = mysqli_connect("localhost", "root", "", "bd_calcadocharme");

$id=$_GET['id'];

$sql="DELETE FROM encomenda WHERE id='$id'";

$result = mysqli_query($link,$sql);

if ($result){
    echo "<script> myFunction('Cliente eliminado com sucesso'); </script>";
    header('refresh:0 ; url=pedir_rese.html'); }
else{
    echo "<script> myFunction('Erro ao tentar eliminar o registo na base de dados!'); </script>";
    header('refresh:0 ; url=pedir_rese.html');
}
?>

2 answers

3

Your if is wrong, it only checks if there is the object and not how many lines have been modified.

In your case I would

$sql="DELETE FROM encomenda WHERE id='$id'";

if ($stmt = mysqli_prepare($link, $sql)) {

    mysqli_stmt_execute($stmt);

    if (mysqli_stmt_affected_rows($stmt) > 0)
    {
        mysqli_stmt_close($stmt);
        echo "<script> myFunction('Cliente eliminado com sucesso'); </script>";
        header('refresh:0 ; url=pedir_rese.html'); }
    }
    else
    {
        mysqli_stmt_close($stmt);
        echo "<script> myFunction('Erro ao tentar eliminar o registo na base de dados!'); </script>";
        header('refresh:0 ; url=pedir_rese.html');
    } 
}

About your problem with informing a user that does not exist depends a lot on the code that is sending the request, a solution is to create a dropdown with only existing customers in the bank and it selects which one it wants to delete. Or else change its message from Erro ao tentar eliminar o registo na base de dados! for Nenhum usuário com esse nome encontrado!.

0

There are two options for you to use.

OPTION 1

You can perform a previous search to the database, checking if the customer exists. If there is the client deletes, otherwise it shows the message that occurred "error" as it does not exist.

$sqlSelect = "SELECT id FROM encomenda WHERE id='$id'";
$data = [];
$result = false;

if($res = mysqli_query($link, $sqlSelect)) {
    while($return = mysqli_fetch_object($res)) {
        $data[] = $return;
    }
}

if(!empty($data)) {
    $sql = "DELETE FROM encomenda WHERE id='$id'";
    $result = mysqli_query($link, $sql);
}

if ($result){
    echo " myFunction('Cliente eliminado com sucesso'); ";
    header('refresh:0 ; url=pedir_rese.html'); 
} else {
    echo " myFunction('Erro ao tentar eliminar o registo na base de dados!'); ";
    header('refresh:0 ; url=pedir_rese.html');
}

OPTION 2

Check how many lines were affected in the last run, if it is greater than 0, then at least one record was deleted. In this case your client has been excluded.

$sql = "DELETE FROM encomenda WHERE id='$id'";

mysqli_query($link, $sql);

if (mysqli_affected_rows($link) > 0){
    echo " myFunction('Cliente eliminado com sucesso'); ";
    header('refresh:0 ; url=pedir_rese.html'); 
} else {
    echo " myFunction('Erro ao tentar eliminar o registo na base de dados!'); ";
    header('refresh:0 ; url=pedir_rese.html');
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.