Problems with insert logic

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 sit field 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 it will insert the sit 1, it follows my structure below.

<?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 it, did not give error more tbm did not register

$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')";
}
    
asked by anonymous 11.09.2015 / 19:54

2 answers

1

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

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

Next, in the IF, you compare a variable with itself, while you should check whether the result of the previous query in the "sit" field 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 your need very well, if it is not clear, comment that I update the answer.

    
11.09.2015 / 20:13
0

Well, in this case it is interesting to do an update query with condition, without having 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 setting field value I execute a sub select, where it takes the last SIT, if it is 1 I put 1, if it's 2 I put 1.

    
11.09.2015 / 20:11