Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Php form with mysql

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Php form with mysql

Postby wesleyheron » Thu Oct 25, 2012 5:49 am

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>
&nbsp;&nbsp;<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?
wesleyheron
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Oct 25, 2012 5:40 am

Re: Php form with mysql

Postby wesleyheron » Thu Oct 25, 2012 6:51 am

Thanks but the problem continues and don't works.
wesleyheron
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Oct 25, 2012 5:40 am

Re: Php form with mysql

Postby faust » Thu Oct 25, 2012 6:55 am

put the
Code: Select all
<?
include_once "cadastoo.php";
?>

outside of the form.
Paste any outputted errors you get, please. This way we may be able to give you more info :)
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Php form with mysql

Postby faust » Thu Oct 25, 2012 6:57 am

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.
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Php form with mysql

Postby wesleyheron » Thu Oct 25, 2012 7:33 am

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.


Faust, I realized the changes u request , but simply when I press the submit button nothing happens.
wesleyheron
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Oct 25, 2012 5:40 am

Re: Php form with mysql

Postby wesleyheron » Thu Oct 25, 2012 9:29 am

Someone help me ? what is wrong in this code?
wesleyheron
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Oct 25, 2012 5:40 am

Re: Php form with mysql

Postby MeroD » Thu Oct 25, 2012 10:27 pm

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
MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: Php form with mysql

Postby wesleyheron » Fri Oct 26, 2012 9:03 am

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
wesleyheron
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Oct 25, 2012 5:40 am

Re: Php form with mysql

Postby MeroD » Sat Oct 27, 2012 12:20 am

MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: Php form with mysql

Postby jinijames » Mon Oct 29, 2012 3:18 am

<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>
jinijames
New php-forum User
New php-forum User
 
Posts: 20
Joined: Fri Oct 19, 2012 4:32 am
Location: India


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.