How to take the value of a Database table field and use it in a PHP variable?

Asked

Viewed 216 times

1

I have the following question: I have a condition if else in php the problem is that I need a value inserted into a field in the database table for the script fulfil the condition of if or of else.

My question is how can I take this value from the bank and move to the variable that will be used in the condition of if else. Below my code, in this case I need the variable "click" to receive the value of the table field of the database.

$clique=?????????

if($clique == 1){
    echo ("<script>alert('ATENÇÃO: Esse registro já foi constado como entregue e não poderá ser inserido novamente!'); location.href = '../../emprestimo.php';</script>");
}
else
{
$query = $conexao->prepare("INSERT INTO memprestimo (idemprestimo, movimento, iditem, nomealuno, email, quantidade) SELECT idemprestimo, 'Emprestimo', iditem, nomealuno, email, quantidade FROM emprestimo WHERE idemprestimo = :idemprestimo");
$query->bindParam(':idemprestimo', $idemprestimo, PDO::PARAM_INT);
$res = $query->execute();            
unset($query);

$query2 = $conexao->prepare("UPDATE emprestimo SET clique=1 WHERE idemprestimo = :idemprestimo");  
$query2->bindParam(':idemprestimo', $idemprestimo, PDO::PARAM_INT);          
$res2 = $query2->execute();
unset($query2);
echo ("<script>alert('Entrega constada com sucesso!'); location.href = '../../emprestimo.php';</script>");
}
  • Take the amount in the bank and put it in the $click, like: $clique = row['coluna'];.

3 answers

0

Try running a new query based on idemprestimo to bring click information, example:

SELECT clique FROM emprestimo WHERE idemprestimo = :idemprestimo

When this is done, assign the value brought from the bank to the variable $clique.

0


I believe you can create a query to retrieve this data from the database table the same way you do to enter or update records. Thus:

$select = $conexao->prepare("SELECT clique FROM tabela WHERE condicao");
$select->execute();
$clique = $select->fetch(PDO::FETCH_ASSOC);

In the case, the clique would be the table field which you want to retrieve the data, the tabela from where you want to recover and the condicao if there is one, if there is not only ignore the where.

  • Hello tried your way but it turns out it always executes only the Else condition, even this condition updating the value of the bank to 1 it keeps entering in Else without considering the updated value of the bank

0

Hello, good afternoon buddy!

First you must declare your variable at the top of the code:

$clique = "";

Then carry out your consultation:

$query = $conexao->prepare("INSERT INTO memprestimo (idemprestimo, movimento, iditem, nomealuno, email, quantidade) SELECT idemprestimo, 'Emprestimo', iditem, nomealuno, email, quantidade FROM emprestimo WHERE idemprestimo = :idemprestimo");
$query->bindParam(':idemprestimo', $idemprestimo, PDO::PARAM_INT);
$res = $query->execute();            

$query2 = $conexao->prepare("UPDATE emprestimo SET clique=1 WHERE idemprestimo = :idemprestimo");  
$query2->bindParam(':idemprestimo', $idemprestimo, PDO::PARAM_INT);          
$res2 = $query2->execute();

Check whether the two queries of your code mysql were met correctly:

if($query && $query2){
    echo ("<script>alert('Entrega constada com sucesso!'); location.href = '../../emprestimo.php';</script>");  
}else{
    $clique = 1;
}

If the consultation mysql is not met, the value of $clique will be changed to 1, thus executing:

if($clique == 1){
    echo ("<script>alert('ATENÇÃO: Esse registro já foi constado como entregue e não poderá ser inserido novamente!'); location.href = '../../emprestimo.php';</script>");
}

NOTE: I did not test the code.

  • Hello, the problem is that these two darlings of mine can only be executed once. By q first an Insert is performed in the loan table and in it the value of the click is started with 0, then enters these two queries of the code where in the first wish I replay the data of the loan table to the table memprestimo and the second query changes the value of the click to 1, ai he would have to perform the second condition q is when the click is with the value 1, it would be basically to prevent the user to enter twice the same thing of the table loan in the table memprestimo

Browser other questions tagged

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