Help with foreign key

Asked

Viewed 76 times

2

I have an intranet system where the user creates a message for different sectors of the company, after creating the message, it can be edited and when it suffers a change, it puts an edited hyperlink at the end of the message, which when clicked, opens a modal with the original message.

So far so good, the problem is when a note suffers more than one change, man select always take the first record, instead of taking all. I will try to describe how it works:

TABLE MESSAGES has (id, message, date, time) and etc. TABLE RECADOS_EDITADOS has (id, id_messages <- foreign key of messages.) etc

EXAMPLE:

Make a INSERT on the table ERRANDS it receives id=31. If I modify this message, a UPDATE on the table ERRANDS and a INSERT on the table RECADOS_EDITY, which receives an id from him and the id=31, which would be the foreign key. If I make another UPDATE on the table ERRANDS, the table RECADOS_EDITY suffers another INSERT with the same foreign key 31.

There when I make one select for example SELECT * FROM RECADOS_EDITADOS WHERE id_recados = 31, he picks up the first record with 31 and not all.

I tried to do it and it didn’t solve:

  $select = mysql_query("SELECT * FROM recadosedit WHERE idRecados = '$id'");
    $v = mysql_fetch_array($select);


    $linhas = mysql_num_rows($select);

    $recadoAnterior = array();
    $dtAlterado = array();
    $hrAlterado = array();


    for($i = 0 ; $i <= $linhas ; $i++){
    $recadoAnterior[$i] = $v['recado'];
    $dtAlterado[$i] = $v['dtAlterado'];
    $hrAlterado[$i] = $v['hrAlterado'];
    }

At the time of showing I also do a go, but it always shows the same record.

 for($i = 0 ; $i <= $linhas ; $i++){echo '

       <div class="row">
        <div class="small-12 medium-12 large-12 columns">
    <label>'.$recadoAnterior[$i].'</label>
    <label>Data alterado:</label>'.$dtAlterado[$i].' <label>Hora alterado:</label>'.$hrAlterado[$i].'


    </div>
      </div>';
      }
  • Wesley, try to break your problem in pieces, first you have to identify if everything is ok in your database, goes into it and selects * from RECADOS_EDITADOS WHERE id_messages = 31 if it returns only one record Voce has to check why it is updating the same record, if it is respecting the Primary key if this id and autoincrement

  • Okay, I did that and he brought four records and in my code I believe he’s taking these four records because in select if I put ORDER BY id DESC he brings a record, if I trade DESC for ASC, he brings another, then I believe he’s taking them all, I just can’t show all to the user

  • this $v is taking all the records ? shouldn’t be with it the mysql_num_rows ?

1 answer

0


Do the following:

$select = "SELECT * FROM recadosedit WHERE idRecados = {$id}";
    if($query = mysql_query($select)){
        if(mysql_num_rows($select)){
            while($resultado = mysql_fetch_array($select)){
                $resultados[] = $resultado; 
            }   
        }   
    }

    if(!empty($resultado)){
        foreach($resultados as $resultado){
            print " Recado: " . $resultado['recado'];
            print " Alterado(data): " . $resultado['dtAlterado']    
            print " Alterado(horas): " . $resultado['hrAlterado']   . "<br/>";
        }   
    }

Well, I don’t have much time, so I’ll be simple and objective.

The problem is how you return the values of this query with the mysql_fetch_array:

$v = mysql_fetch_array($select);

In this case, you only store the value of the first column of the results found in this query. For this, you only need to enter the mysql_fetch_array in a loop, as in the example above:

while($v = mysql_fetch_array($select))
{
...
}

Why should we not use mysql type functions_*?

  • Edilson, Thanks for the great help, your code worked, I just had to change the if(! Empty($result)) by if(! Empty($results)).

  • But when I click on the first edited message it shows all changes, when I click on the second it shows all changes of the first and second.

  • Look, maybe I misunderstood the problem, you can explain yourself better ?

  • Next, there’s an errand page, on this page he makes a select to fetch all messages that are open, and I use the while to show the message, inside that while has an If to check if that message has been edited, if it has already been edited it shows the message and a hyperlink written edited, where the user clicks the edited and appears the previous message in a modal, where enters the code you helped me, which by the way worked, the only problem that if I click the message below, It shows his editing and the messages that are above.

  • I put this at the end of the for: unset($results); and solved, thanks for helping Edilson

  • Maybe because you were reusing that same variable.

Show 1 more comment

Browser other questions tagged

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