Error saving Mysql PHP form information

Asked

Viewed 465 times

0

What’s going on:

I have a system that, after completing the form and saving, does not save the changes by appearing a alert "saved without changes" and back to previous page.

What should happen:

When trying to save the form, the Save script should appear successfully and return to the previous page.

Since I don’t understand this language, I’m not finding where the logic is responsible for recognizing that a field has been changed and saving. I believe the error is in the form, since there is nothing wrong with the bank or the connection. Since I can’t put the whole system here, I’ll put some parts that I believe may be responsible for the mistake:

Find the ID of what you want to change

<?php
// Busca valores no banco de dados para preenchimento do formulario para alterar
$id = $_GET['id'];
$sql = (" SELECT * FROM re WHERE id = '{$id}'");
$query=mysql_query($sql);
while ($campo = mysql_fetch_assoc($query)) {  
?>

part of the form

<div class="container">
    <form method="post" action="../std/update.php" enctype="multipart/form-data" name="re" class="formulario">
        <div class="container container-divisor">
            <h4>Descrição Re</h4>
            <div class="row">
                <input type="hidden" name="idnovo" id="idnovo" value="<?=$campo['id'];?>">
                <input type="hidden" name="dataedicao" id="dataedicao" value="<?php echo date('d/m/Y'); ?>">
                <div class="col-md-4">
                    <label for="datacriacao">Data</label>
                    <input type="text" class="form-control" name="datacriacao" id="dataCriacao" value="<?=$campo['datacriacao'];?>"127>
                </div>
          </div>
    </form>

On the page responsible for updating the database, the texts in js:

<?php
    if(mysql_affected_rows() > 0){
    ?>  
<script type="text/javascript">
    alert("Salvo com Sucesso !");
    window.history.go(-1);
</script>

<?php
        }
        else{
?>
<script type="text/javascript">
    alert("Salvo sem Modificações !");
    window.history.go(-1);
</script>
<?php           
        } 
mysql_close($conexao);
?>

The update page

<?php 
include ("conecta.php");

//coletando dados do formulario

    $id             =   $_POST["idnovo"]; 
    $dataedicao     =   $_POST["dataedicao"];
    $datacriacao    =   $_POST["datacriacao"];
    $statusgeral    =   $_POST["statusgeral"];
    $criadopor      =   $_POST["criadopor"];


// Inserir dados no banco
    $itens = $_POST['meti'];
//$_POST['meti'] as $itens
    if (!empty($itens)){
 $itens = implode(',', $_POST['meti']);
}



$up= mysql_query("UPDATE retex SET dataedicao = '$dataedicao', datacriacao = '$datacriacao', statusgeral = '$statusgeral', criadopor = '$criadopor' WHERE id = '$id' ");

?>
<?php
    if(mysql_affected_rows() > 0){
    ?>  
<script type="text/javascript">
    alert("Salvo com Sucesso !");
    window.history.go(-1);
</script>

<?php
        }
        else{
?>
<script type="text/javascript">
    alert("Salvo sem Modificações !");
    window.history.go(-1);
</script>
<?php           
        } 
mysql_close($conexao);
?>

I believe the form is not acknowledging that a field has been altered, but it is only a hypothesis. As I discover more things I edit.

  • 1

    Friend, do not use mysql is already discontinued, use mysqli and there is something else here use so $sql = "SELECT * FROM re WHERE id = '{$id}' "; if not php consider as text, try this and return, hugs

  • 1

    Mariana after while change your question and paste all the update code. Without that part there is not much to help

  • Then, changing all mysql_query to mysqli_query, both in the update, both in the connection and in the form page, the content disappeared, giving the message "NULL" -@Andersonhenrique

  • 1

    I think it would be better if you separate everything. php for one file, html for another and javascript for another. It is more readable the code

  • So, the scripts are at the bottom of the update page, responsible for the bank. The page changes is the page responsible for mounting the form and the code that recognizes the ID is at the beginning of it. That is, two-page codes were posted.

  • @adventist .

  • type javascript is inside php. Also try using mysqli as suggested by @Andersonhenrique

  • So I can’t switch to mysqli because of my old php version.

  • In your update you have the following SET dataedicao = '$dataedicao',' has a comma after single quotes ',' Remove the comma and single quotes to the right. What would be SET dataedicao = '$dataedicao'

  • I edited and put more information in the form and the bank to understand how I was doing (Before it was a mistake to type the question. apology)

  • Is your connected file normal? You are selecting the database?

  • Yes, the database is connecting, so much so that when I select a form id it loads all the information from the last edit. Just don’t save the new editions.

  • try to echo each field to see if the id is going.. because if I didn’t have id it doesn’t change

  • The ID goes, so much so that the last edits made, saved in that ID, load. Only the field is not recognizing that something was typed in it. and so appears the Alert of "saved without modifications".

Show 10 more comments
No answers

Browser other questions tagged

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