Error saving converted date in phpMyAdmin database

Asked

Viewed 85 times

0

I am trying to write a form in the database, in this form has a date field that contains mask dd/mm/aaaa. To record in the bank I need to convert that date to aaaa-mm-dd, so far I’ve managed str_replace, the problem is that when I go to record this variable in the bank it returns me an error and does not write anything in the bank. Follow me code:

<form action="envia.php" method="post" enctype="multipart/form-data">
        <div class="form-row">
            <div class="form-group col-md-6">
                <label class=""><b>Sistema</b></label>
                    <select name="sistema" class="form-control" required/>
                        <option selected>Escolha o sistema</option>
                        <option value="ATPH">ATPH </option>
                        <option value="DPPH">DPPH Pagamento</option>
                        <option value="EFPH">EFPH </option>
                        <option value="SCPH">SCPH</option>
                        <option value="GEPH">GEPH</option>
                        <option value="BICLI">BIPH</option>
                        <option value="BISER">BIPH</option>
                    </select>

            </div>

            <div class="form-group col-md-6">
                <label class=""><b>Data</b></label>
                <input type="text" name="data" class="form-control" maxlength="10" onkeypress="mascaraData(this)" placeholder="Digite a data da Revisão" required/>
            </div>  

            <div class="form-group col-md-6">   
                <label class=""><b>Versão</b></label>
                <input type="text" name="versao" class="form-control" placeholder="Digite a Versão" required/>
            </div>

            <div class="form-group col-md-6">
                <label class=""><b>Revisão</b></label>
                <input type="text" name="revisao" class="form-control" placeholder="Digite a Revisão" required/>    
            </div>  

            <div class="form-group col-md-12">
                <label class=""><b>Arquivo</b></label>
                <input type="file" name="arquivo" class="form-control" required/>
            </div>

            <div class="form-group col-sm-2">
                <div class="">
                    <button type="submit" class="btn btn-primary upload">Enviar</button>
                </div>
            </div>
        </div>
    </form>  


SQL


Estrutura do banco

  • Have you tried to var_dump the $query variable, just to make sure that during the application it is receiving the parameters correctly? By the way, now I noticed something, the formatting vc uses in echo and not in variable. That is your variable is only recorded 07-11-2018 for example.

  • Dude, so what I tried to do was the guy below responded, and I put an echo to read the ($data), According to the print (http://prntscr.com/lfhn8i) it reads the date in the correct format, but falls on the same Lse below and does not write. I’m half beginner in sql, how can I use this var_dump ?

  • var_dump would be in php itself, below your query do so: var_dump($query); Ai comments the line of the result and if. Ai only makes the impression of this var_dump on the screen to see how it is receiving the query

  • (http://prntscr.com/lfhvgn). Is that right? Print nothing on canvas.

  • UPDATING: I found an array on the internet that converts this date, conf rme print : ( http://prntscr.com/lfiqyc ). After this change, I tried to send it to the bank, it returns this message ( http://prntscr.com/lfiqgw ) which is o echo ($query);, and does not record anything in the bank and returns the same LSE as before. I still haven’t found a solution.

  • Sorry for the delay my dear, I was on the street all afternoon. Interesting, apparently your variable is receiving correctly. The first 2 values "version" and "review" are recording normally in the database?

  • 1

    I did the tests here, I think it is the format of string that is disturbing, put everything between parentheses leave your query like this: $query = "INSERT INTO $table (versao, revisao, data) VALUES ('$versao', '$revisao', '$data')";

  • I did, with the guy’s answer down there, but thank you very much for your help

Show 3 more comments

2 answers

1


I believe your problem is just how you inserted the variables into the string that will be sent to the database. Try this way:

$query = "INSERT INTO ".$table." (versao, revisao, data) VALUES ('".$versao."', '".$revisao."', '".$data."')";

It’s ugly, it’s boring to write, but I think it solves the problem. The values must be inside quotes but as it comes from a variable it must be outside a string, so vc closes the string and concatenates the values, one by one.

  • Dude, it worked just right, the way the Internet was written was wrong ? Or are there several ways ?

  • I know two ways, the one you used that lists first the columns and then the values that go in them. The other would be like this: $query = "INSERT INTO table SET column='value', column2='value2'"; But that’s not the question, your query was correct. The problem is the use of single quotes ' and quotation marks ". This post contains information on the subject: https://answall.com/questions/4652/diff%C3%A7a-between-quotes-single-and-double-in-php In short, I always concatenate, as I did in the answer example.

1

If you are printing the date value correctly, make the variable receive this value. See:

$data = date('Y-m-d', strtotime($data));

Of course, after you have used str_replace

  • http://prntscr.com/lfh0lg I tried to do what you said and then tried to print to see if it worked and still not recording and falling into else. (http://prntscr.com/lfh250) printed this that ta in the print, the date that came from the form was 06/12/2018, so he converted right only unsaved in the bank.

Browser other questions tagged

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