Hi there,
I am new in PHP universe and now i have some mistakes in my code:
First code:
formulario.php ( the simple form to input data)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br">
<head>
<meta name="author" content="Marcelo Torres" />
<meta http-equiv="content-type" content="text/html; charset=iso-utf-8" />
<title>Formulário de contato em PHP</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<form action="formulario.php" method="post" >
<fieldset style="width: 50%; height: 50%;">
<legend>Ficha de Inscrição</legend>
<small>*Atenção: É obrigatório o preenchimento de todos os campos do formulário para validar a inscrição.</small><br />
<?
include_once "cadastoo.php";
?>
<label for="nome">Nome:</label>
<input id="nome" name="nome" size="31" type="text" /><br />
<label for="assunto">CPF:</label>
<input id="cpf" maxlength="11" name="cpf" size="11" type="text" /><br />
<label for="email">Email:</label>
<input id="email" name="email" size="31" type="text" /><br />
<label for="nome">Telefone:</label>
<input name="telefone" id="telefone" type="text" size="10" /><br />
<label for="nome">Matricula:</label>
<input id="matricula" name="matricula" size="8" type="text" /><br />
<label for="nome">Evento:</label>
<input id="evento" name="evento" type="text" size="31" /><br />
<label for="nome">Horário:</label>
<input name="horario" id="horario" type="text" size="10" /><br />
<input name="enviar" type="submit" value="Enviar inscrição" class="botao"/>
<input name="cancelar" type="reset" value="Limpar" class="botao"/>
</fieldset>
</form>
</body>
</html>
config.php (the settings of my database).
<?
$hostname= 'localhost'; // seu servidor
$usuario= 'root'; // usuario de seu banco de dados
$senha= ''; // senha do usuário
$banco= 'BD_INTRA'; // Banco de dados que será utilizado
mysql_connect ( $hostname, $usuario, $senha)or die(“Não conseguiu conectar: “.mysql_error());
mysql_select_db($banco,$hostname) or die(mysql_error());
?>
cadastroo.php (here the code to record in my database)
<?
if(isset($_POST['enviar'])){
include_once 'config.php';
$nome = isset($_POST['nome'])?$_POST['nome']:null;
$cpf = isset($_POST['cpf'])?$_POST['cpf']:null;
$email = isset($_POST['email'])?$_POST['email']:null;
$evento = isset($_POST['evento'])?$_POST['evento']:null;
$telefone = isset($_POST['telefone'])?$_POST['telefone']:null;
$turno = isset($_POST['horario'])?$_POST['horario']:null;
$matricula= isset($_POST['matricula'])?$_POST['matricula']:null;
$query="INSERT INTO cadastro (nome,cpf,email,telefone,matricula,evento,turno) VALUES('$nome','$cpf','$email','$telefone','$matricula','$evento','$turno')";
$res = mysql_query($query) or die(mysql_error());
mysql_close();
echo "Você foi registado com sucesso!";
}
?>
I don't know why do not record in my database ? what is wrong?
Php form with mysql
Moderators: egami, macek, gesf
put the
outside of the form.
Paste any outputted errors you get, please. This way we may be able to give you more info
Code: Select all
<?
include_once "cadastoo.php";
?>
Paste any outputted errors you get, please. This way we may be able to give you more info

one more thing. I think this may be the problem but cannot be sure.
$nome = isset($_POST['nome'])?$_POST['nome']:null;
If you have a field with a null value, in the database you may have checked NOT NULL and this may be causing the problem.
$nome = isset($_POST['nome'])?$_POST['nome']:null;
If you have a field with a null value, in the database you may have checked NOT NULL and this may be causing the problem.
Faust, I realized the changes u request , but simply when I press the submit button nothing happens.faust wrote:one more thing. I think this may be the problem but cannot be sure.
$nome = isset($_POST['nome'])?$_POST['nome']:null;
If you have a field with a null value, in the database you may have checked NOT NULL and this may be causing the problem.
1. Turn on error reporting to its maximum level while you are developing: error_reporting(E_ALL);
2. Fix mysql:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("yourdb", $link);
echo mysql_errno($link) . ": " . mysql_error($link). "\n";
3. Agrees with faust. Indiscriminately Inserting null values may cause a problem.
Use: DESCRIBE yourtable; to check for NOT NULL. You can run this from the MySQL CLI; phpMyAdmin
2. Fix mysql:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("yourdb", $link);
echo mysql_errno($link) . ": " . mysql_error($link). "\n";
3. Agrees with faust. Indiscriminately Inserting null values may cause a problem.
Use: DESCRIBE yourtable; to check for NOT NULL. You can run this from the MySQL CLI; phpMyAdmin
MeroD wrote:1. Turn on error reporting to its maximum level while you are developing: error_reporting(E_ALL);
2. Fix mysql:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("yourdb", $link);
echo mysql_errno($link) . ": " . mysql_error($link). "\n";
3. Agrees with faust. Indiscriminately Inserting null values may cause a problem.
Use: DESCRIBE yourtable; to check for NOT NULL. You can run this from the MySQL CLI; phpMyAdmin
OK I fixed my code, but give erro:
0: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','wesley.heron@hospitalsaodomingos.com.br','91622037','003922','Palestra 1')' at line 2
http://dev.mysql.com/doc/refman/5.5/en/insert.htmlYou have an error in your SQL syntax;
http://www.w3schools.com/php/php_mysql_insert.asp
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>