Problems with Insert logic

Asked

Viewed 123 times

1

I have a problem. I’m doing an event access control system, I created a table that records the given id,code,date,time,sit, the field sit will be 1 and 2, but I need my system to understand the following. When the last record of the bank is 1 it will insert the sit 2 and when it is 2 will insert the sit 1, follows below my structure.

<?php
    include"conect_db.php";

    $string_sql = mysql_select_db("evento"); //seleciona o banco de dados

    //Abaixo atribuímos os valores provenientes do formulário pelo método POS 
    $string_sql = ("select * from checkout where codigo='06181121978' order by id desc limit 1");
    mysql_query($string_sql,$conn); //Realiza a consulta

    if ($string_sql==$string_sql) {
        $teste = ("INSERT INTO checkout (id,codigo,data,hora,sit) VALUES (null,'06181121978',CURDATE(),curtime(),'1') <> (select * from checkout where codigo='06181121978' AND SIT='2' order by id desc limit 1");
        mysql_query($teste,$conn);
    }
    else{
        $teste2 = ("INSERT INTO checkout (id,codigo,data,hora,sit) VALUES (null,'06181121978',CURDATE(),curtime(),'2')");
        mysql_query($teste2,$conn);
    }
?>

I did so , gave no error more tbm not registered

$string_sql = mysql_select_db("evento"); //seleciona o banco de dados

//Abaixo atribuímos os valores provenientes do formulário pelo método POS 


$string_sql = ("select * from checkout where codigo='06181121978' order by id desc limit 1");
 mysql_query($string_sql,$conn); //Realiza a consulta

$resultado = mysql_query($string_sql, $conn);

$dado = mysql_fetch_assoc($resultado);

if ($dado['sit'] == '1') {
    // insere sit 2
    "INSERT INTO checkout (id,codigo,data,hora,sit) VALUES (null,'06181121978',CURDATE(),curtime(),'2')";
}
else {
    // insere sit 1
    "INSERT INTO checkout (id,codigo,data,hora,sit) VALUES (null,'06181121978',CURDATE(),curtime(),'1')";
}
  • if ($string_sql==$string_sql) { ? Missed to take the result of the query and apply on the if, Cade o mysql_fetch_assoc() :P

2 answers

1

There are some errors in your logic. The function mysql_query() should be assigned to a variable that receives its output, for example:

$resultado = mysql_query($string_sql, $conn);

Just below, in IF, you compare a variable with itself, while you should check if the result of the previous query the field "sit" is 1 or 2, for example:

$dado = mysql_fetch_assoc($resultado);
if ($dado['sit'] == '1') {
    // insere sit 2
}
else {
    // insere sit 1
}

I do not know if I understood very well your need, in case it is not clear, comment that I update the answer.

0

Well, in this case it is interesting to do an update query with condition, without needing to impose logic within the scope of your script.

An example:

UPDATE sua_tabela
SET sit = CASE
WHEN (SELECT sit FROM sua_tablea WHERE sua_condicao ORDER BY id DESC LIMIT 1 ) = 1 THEN 2
ELSE 1
END

In this example I run the update, when defining the value of the field I run a sub select, where I take the last SIT, if it is 1 I put 1, if it is 2 I put 1.

  • more I will use in a form that will receive the cidigo bars, will work?

  • I don’t understand your doubt, Fernando. In the UPDATE query I just ran a sub select by taking the SIT of the last record and doing an IF ELSE for the field value. Basically I removed all this script you made to run 3 querys to do this, I left the charge of MYSQL.

  • Hello, I managed to do, the problem was that when I created the condition I had not created a variable for the Insert inside if and Else, so I did not save the data. if ($given['data'] == '1') { // inserts sit 2 $query ="INSERT INTO Lanca (id,data) VALUES (null,'2')"; $result = mysql_query($query) or die('Error, query failed'); } Else { // inserts sit 1 $query ="INSERT INTO Lanca (id,data) VALUES (null,'1')"; $result = mysql_query($query) or die('Error, query failed'); }

Browser other questions tagged

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