2
I am a beginner in the forum and in php and I have a question. I have a table with id auto_incremento and name is primary key. There is how I do IF and ELSE using UPDATE and INSERT. Where, if there is name only do update, otherwise do Insert ?
<?php
$sql = "SELECT * FROM usuarios";
if(isset($_POST['enviar'])){
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$result_dados_pessoais = "INSERT INTO usuarios (nome, cpf) VALUES ('$nome', '$cpf')";
$resultado_dados_pessoais= mysqli_query($conn, $result_dados_pessoais);
//ID do usuario inserido
}
?>
the first way changed all records of the kkkk database
– Victor
@Victor vc had to have completed the update with Where clause, I did not imagine that you did not know that. I edited the answer
– user60252
I had put two clauses Where KKK was for this, but then fixed, actually I can put two clauses right but in a table that contains more fields
– Victor
This is wrong. Another thing, if you check if the name exists and it exists because update the name, it would not only be in Cpf?
– user60252
Yes, I did that is that I created it just to exemplify and get short, but I have fields like : Product Description, Product Code, Unit of Measure that is sold, Reference Code... and then I wanted to know if I could use two Where clauses, I think it should give
– Victor
@Victor ... where name='$name' AND
cpf
='$Cpf'– user60252