If there is data in mysql make a PHP / MySQL update

1

Well, I suppose this question is recurrent. I even tried some alternatives but they did not work.

I have a Screen where I record Service Orders with the Client and PC data. It turns out that. I am filling 2 Tables simultaneously.

In the tb_os table I record the Service Orders itself (complete). And in the clientes table I only record client information if I just want to make my client list (which I can use elsewhere in my system).

But since my client table is already getting a little big, I did not want to spend all the time running the risk of registering the same client, with the same name, address, etc.

My functional code is this:

 $sql = " insert into tb_os (os_status, cliente, telefone, celular, email, endereco, endereco_num, complemento, bairro, cep, tipo, marca, modelo, processador, memoria, hd, acompanha, ordem_servico )";
$sql.= " values ( '$os_status', '$cliente', '$telefone', '$celular', '$email', '$endereco', '$numero', '$complemento', '$bairro', '$cep', '$tipo', '$marca', '$modelo', '$processador', '$memoria', '$hd', '$acompanha', '$texto_os' ) ";

sql2 = " INSERT INTO clientes (cliente, email) VALUES ('$cliente', '$email') ";

 if (mysqli_query($link, $sql) AND mysqli_query($link, $sql2)){ 
    $mail = $email;     
    $to = $email;

Where I conclude with an email to the customer. So far right filling out the 2 tables.

I tried this to prevent it from popping up with repeated information:

if(mysqli_query($link, "SELECT * FROM clientes WHERE cliente = '".$cliente."'")) {
    $sql2 = "UPDATE clientes Set cliente = '".$cliente."', telefone = '".$telefone."' WHERE cliente = '".$cliente."' ";
} else {
    $sql2 = "INSERT INTO clientes (cliente, telefone) VALUES ('".$cliente."', '".$telefone."')";
}

But by doing this, it simply ignores my request for update in clientes and registers the tb_os tables in a normal way.

In these tables as primary keys I have id_os in tb_os and id_cliente in clientes . But I do not know if this helps in any way.

Can anyone give me a light? Is there a way to check if the registry with the Client name already exists, if there is only an update?

I do not know what to do.

    
asked by anonymous 15.10.2017 / 18:22

2 answers

1

This is not the correct command to check if it already exists in the table. The correct command is this:

mysqli_num_rows()

How would you look?

if(mysqli_num_rows(mysqli_query($link, "SELECT * FROM clientes WHERE cliente = '".$cliente."'")) > 0) {
    $sql2 = "UPDATE clientes Set cliente = '".$cliente."', telefone = '".$telefone."' WHERE cliente = '".$cliente."' ";
} else {
    $sql2 = "INSERT INTO clientes (cliente, telefone) VALUES ('".$cliente."', '".$telefone."')";
}

Source: w3schools

    
15.10.2017 / 19:26
0

Good afternoon Rafael! There is always that moment after the family's lunch that everyone sleeps, so we can take a nap here KKK Look, your problem is similar to one that I answered a few days ago, I suggested that a function be created in the database that tries to update the registry and if it has not affected anything, do the insertion. This makes the application just need to call the function in the database and the database turns with the problem solution. The resolution in the post is in PostgreSQL, make one for MySQL that will work as well. If you need help to set up the function send me a comment here that I change when I have access to a pc, now I'm on my cell phone.

link

    
15.10.2017 / 18:42