Doubt - Update in SQL from PHP form

0

I have a table in which columns 1 through 5 are fixed data that I feed into SQL and it appears the listing on the WEB page. In this WEB page I have a form in which the user should answer 3 questions, in inputs in columns 6 to 8.

It turns out that the worksheet is updating all the rows with the data answered in the last line. how do I fix it?

My update function:

function insereDado($conexao, $id, $campo6, $campo7, $campo8) {
$query = "update dbo.avalia_pf2 set campo6 = '{$campo6}', campo7 = '{$campo7}', campo8 = '{$campo8}' where id= {$id}";
$resultadoDaInsercao = mssql_query($query, $conexao);
return $resultadoDaInsercao;

My variables:

$id = 'id';
$campo6 = $_POST["campo6"];
$campo7 = $_POST["campo7"];
$campo8 = $_POST["campo8"];

My form:

<form action="adiciona-dado.php" method="post">
                <table class="table table-striped">         
                    <thead>
                        <tr>
                            <th class="titulo_cell">Título 1</th>
                            <th class="titulo_cell" >Título 2</th>
                            <th class="titulo_cell" >Título 3</th>
                            <th class="titulo_cell" >Título 4</th>
                            <th class="titulo_cell" >Título 5</th>
                            <th class="titulo_cell" >Pergunta 1</th>
                            <th class="titulo_cell" >Pergunta 2</th>
                            <th class="titulo_cell" >Pergunta 3</th>
                        </tr>
                    </thead>

                    <?php $dados = listaDados($conexao); foreach($dados as $dado) : ?>
                        <tbody>
                            <tr>
                                <td name="campo1"> <?php echo $dado['Campo1']; ?> </td>
                                <td name="campo2"> <?php echo $dado['Campo2']; ?> </td>
                                <td name="campo3"> <?php echo $dado['Campo3']; ?> </td>
                                <td name="campo4"> <?php echo $dado['Campo4']; ?></td>
                                <td name="campo5"> <?php echo $dado['Campo5']; ?> </td>
                                <td><input type="text" name="campo6" class="form-control"><? $dado['Campo6']?></input></td>
                                <td><input type="text" name="campo7" class="form-control"><? $dado['Campo7']?></input></td>
                                <td><input type="text" name="campo8" class="form-control"><? $dado['Campo8']?></input></td>
                                <td><button type="submit" class="btn">Incluir</button></td>
                            </tr>
                        </tbody>
                    <?php endforeach ?>
                </table>
            </form>

Important information: my ID is "NOT NULL" (not primary key with auto increment) How do I fix UPDATE? It is some information that I pass at the time of declaring the variable $ id but I am not able to reach the result.

    
asked by anonymous 30.05.2017 / 19:39

2 answers

0

Well, let's get down to basics, When you call a function are you passing the ID? if it does, it gives a var_dumb ($ id) inside its function and sees if it returns the id number, maybe the function is not receiving the id and therefore is giving problem.

    
30.05.2017 / 20:26
0

1) You need to create a hidden field with the value of your id in the form, such as <input type="hidden" name="id" value="<?=$id_do_registro?>">

2) Retrieve this id on the next page by $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);

Notice that I used the filter_input function to retrieve the value. You should use this filter whenever you retrieve user-submitted variables for security purposes. Read more at link

    
30.05.2017 / 20:55