Doubt - SQL update from PHP form

Asked

Viewed 481 times

0

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

It turns out that the spreadsheet is updating all the lines with the dice answered in the last line. how do I fix?

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’s some information I pass when declaring the $id variable but I’m not getting the result.

2 answers

0


Well, let’s go through the basics, When you call insedeDados function you are passing the ID? if yes, give a var_dumb($id) inside its function and see if it returns the id number, maybe the function is not receiving the id and so is giving problem.

  • I could not understand, where I insert the var_dumb($id)

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

  • Hasn’t worked yet.

  • How do you assign the value of the $id variable and then send it to the function?

  • $id = 'id'; I also tried $id = $_POST["id"]; and $id = $_GET["id"];

  • When you give a submit in your form, it will take all that information and send it to the add-data.php page, right? well, on this page I imagine you have: `

  • When you give a submit in your form, it will take all that information and send it to the add-data.php page, right? well, on this page I imagine you have: ` $campo6 = $_POST["campo6"]; .. You need to have something referencing the ID can be $id = 4 if you want to manually pass the id value to the variable, or $id = $_POST['id'] if it’s coming from there your form. From what I saw in your code you didn’t.

  • Hi... so the ID does not come from the form, I import a spreadsheet to SQL, columns 6, 7 and 8 have inputs to answer by form..

Show 3 more comments

0

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

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

Notice that I used the function filter_input to recover the value. You should use this filter whenever retrieving user-sent variables, for security. Read more on http://php.net/manual/en/function.filter-input.php

  • Rafael, but in case the ID is not sent by the user... I inform the ID number when sending the spreadsheet to SQL.

  • So I suggested the field hidden, which is to be populated with the value you know. In the example you sent ID is permanently with the string value 'id' and, with this, your query ends up updating all the records of the database, because its clause WHERE will be transformed into WHERE id = id, which is always true. It is necessary that you pre-fill the field hidden with the ID value that comes from the database and retrieves it on the next page with the corresponding value. And so your ID variable will have the correct value.

  • You can open the form page by passing the id by query string form.php?id=321 and, in the field Hidden, use this value, as in $id_do_registro = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); and presenting in html as <input type="hidden" name="id" value="<?=$id_do_registro?>">

Browser other questions tagged

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