If there is any data in mysql do a PHP/Mysql update

Asked

Viewed 749 times

1

Guys, I imagine this doubt is recurring. I even looked for some alternatives but did not work.

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

On the table tb_os i record The Service Orders themselves(complete). And in the table clientes i only record customer information in case I want to make only my customer list(which I can use elsewhere in my sisteminha).

But as my client table is already getting a little big, I didn’t want to spend all the time at risk of registering the same customer, 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 sent to the client. Until then I complete the 2 tables.

Then I tried it to prevent it from populating 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 in doing so, he simply ignores my request to update in clientes and register the tables tb_os normally.

In these tables as primary keys I have id_os in tb_os and id_cliente in clientes. But I don’t know if it helps at all.

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

Hug guys and good Sunday leftover if there’s someone walking around here today kkkk.

2 answers

2

Good afternoon Rafael! There is always that moment after the family lunch that everyone sleeps, so we take the time to have a drink here kkk Look, your problem is similar to one 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 only need to call the function in the database and the database manages to solve the problem. The resolution in the post is in Postgresql, make one of these to Mysql that will work as well. If you need help to assemble the function send me a comment here that I change when I have access to a pc, now I’m by cell.

/a/245627/51460

  • Programmer really has a differentiated watch kkkkkkkkkk I ended up using the tip of Leo, as I do not yet know of database functions. But I already got the link here to study, because the idea is very good (And practice too, even for a more complete code). Thanks for the help Renan. Have a great week!

1


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

mysqli_num_rows()

How would it 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

  • 1

    Oh true, could have made the query by checking number of lines. Thanks my expensive worked in a very good way.

Browser other questions tagged

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