1
I never made a form, so forgive me if I’m absurdly wrong.
Here is the bank with the table:
CREATE DATABASE db_formacao;
USE db_formacao;
CREATE TABLE `db_formacao`.`formacoes` (
`ID` INT( 5 ) NOT NULL AUTO_INCREMENT ,
`NOME` VARCHAR( 255 ) NOT NULL ,
`OBJETIVO` TEXT NOT NULL ,
`CARGA` INT( 5 ) NOT NULL ,
`CONTEUDO` TEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) ENGINE = MYISAM ;
Already here, on the page itself that the form is being built try to make the connection:
<?php
$conn = new mysqli ("localhost", "root", "", "db_formacoes");
$nome = $_POST['nome'];
$objetivo = $_POST['objetivo'];
$conteudo = $_POST['conteudo'];
$carga = $_POST['carga'];
$squery = "INSERT INTO formacoes (nome, objetivo, conteudo, carga) VALUES('$nome','$objetivo','$conteudo', '$carga' )";
$resultado = mysqli_query($conn,$squery);
if(!mysqli_query($conn, $squery)){
echo 'Opa! Não conseguimos nos conectar ao banco de dados. '. mysqli_error($conn);
}else{
echo 'Operação realizada com sucesso';
}
mysqli_close($conn);
?>
When I click on the save button of the form nothing happens to indicate that the information was saved or not and I am checking this by selecting in phpMyAdmin. Follow the found result:
Before you start, you named the database as
db_formacao
but useduse formacao
, without the prefixdb_
. Is this correct? The functionmysqli_connect
receives four parameters, including server path, user, password and database name. You passed only the first one. And you also did not correctly realize the values in$sql
.– Woss
Oops, I don’t know. Let me change and see if it works.
– Mariana Bayonetta
Are you sure you can connect? In mysqli_connect there is no login, pass and bank name
– Anderson Henrique
missing define the database, login, and password, and the name of the created table was
formacoes
in the query you useddb_formacao
– Rovann Linhalis
@Marianaferreira there are several errors in the question, one of them the @Andersoncarloswoss already commented. Another thing: there in INSERT INTO, you are using
db_formacao
instead ofdb_formacao.formacoes
and then right away, you do$sql =
instead of doing$sql .=
to concatenate thestrings
.– FabianoLothor
So the bank doesn’t have a password.
– Mariana Bayonetta
Yes but even not having a password have the root login and in place of the password enter ""
– Anderson Henrique
You can edit the question so I can see how exactly I should change my code?
– Mariana Bayonetta
I’ll edit the code for the changes I made.
– Mariana Bayonetta
Great is improving, doubt you’re getting the data in the post? already gave an echo to see? there where you have the $sql you can put together it is not necessary to separate, causing it to look like $sql = "INSERT INTO db_formacao.formacoes(NAME,OBJECTIVE,LOAD,CONTENTS) VALUES ('{$NAME}', '{$OBJECTIVE}', '{$LOAD}' ,'{$CONTENTS}')"; These keys are to determine that it is a variable
– Anderson Henrique
@Marianaferreira saw that he edited the code of the question, but did not edit the texts. Can you describe what is happening now when you run this code? Is there an error? The record is registered?
– Woss
So nothing comes forward to confirm whether or not the form was registered. I am checking a select in the table by phpMyAdmin, but it says that the table is empty, so it was not registered.
– Mariana Bayonetta
@Marianaferreira I strongly recommend you read How to write values to the MYSQL database using PHP?
– Woss
@Did you get it? Please make the changes I asked for if you did not follow and I’m sorry I forgot to put the name of the database in this line mysqli_connect('localhost', "root", "",db_formacao)
– Anderson Henrique
I made the changes you suggested and unfortunately it still doesn’t work. I am reading the link sent in the previous comment. Thank you. :)
– Mariana Bayonetta
Then, the message I had left to appear in case of this error in the database connection appears as soon as I enter my form page, even before I try to register something. That is, for some reason the bank does not connect.
– Mariana Bayonetta
@Andersonhenrique
– Mariana Bayonetta
Could you provide the code for how you are ? @Marianaferreira
– Anderson Henrique
Yeah, I’ll edit it out
– Mariana Bayonetta