Jump to content

[Resolvido] fico com Campos vazios


Blue_Acid
 Share

Recommended Posts

boas ppl, comecei agora a dar uns toques na programação PHP, e queria começar por uma sistema de Newsletters a parte do registo, já fiz a base de dados apenas com os campos "Nome" e "Mail", tinha o campo "ID" de auto incremento, mas tirei pk pensei k fosse isso k m estava a avariar o script.

Tenho apenas duas páginas o index, onde se adiciona o nome e o mail, e faz logo a listagem dos inscritos, e tenho o ficheiro PHP k adiciona, alguem me pode verificar o codigo e dizer-me pk fico com os campos vazios. Obrigado

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>adicionar news</title>
</head>

<body>
<form method="POST" action="add_new.php">
  <label>Nome
  <input type="text" name="nome"  />
  </label>
  <label>e-mail
  <input type="text" name="mail" />
  </label>
  <label>
  <input type="submit" name="Enviar" value="Enviar" />
  </label>
</form>

<?
$msg[0] = "Erro ao Conectar";
$msg[1] = "Erro ao aceder a Base de Dados";

//Ligar a Base de Dados
$connect = mysql_pconnect ("localhost","admnews","adm") or die ($msg[0]);
mysql_select_db("newsl", $connect) or die ($msg[1]);
?>

Listagem de Associados:
<table border="1">
<tr>
  <td>Nome</td>
  <td>E-Mail</td>
</tr>
<?
// completar a tabela
$query = "SELECT nome,mail FROM add_new";
$resultado = mysql_query($query,$connect);
while ($linha = mysql_fetch_array ($resultado))
{
	?>
<tr>
	<td width="200"> <? echo $linha ['nome'];?></td>
	<td width="600"> <? echo $linha ['mail'];?></td>
</tr>
<?
}
?>
</table>
</body>
</html>

Este é o PHP k adiciona

<?
global $nome;
global $mail;

$nome = trim($nome);
$mail = trim($mail);


$connect = mysql_connect("localhost","admnews","adm") or die ("Erro a ligar a BD");
$insert = "INSERT into add_new values ('$nome','$mail')";
mysql_db_query("newsl", $insert, $connect);

header("Location: index.php");

?>

Obrigado Pessoal, bom fim de semana  🙂

SIGNIFICADOS: WWW: Wait,wait,wait... HTML: Hoje Ta Mais Lento.KEYBOARD: Dispositivo usado para entrada de erros no Computador.

Link to comment
Share on other sites

ok, então eu fiz assim um file SQL tb para começar a brincar com isto.

create table add_new(
ID int not null auto_increment primary key,
nome varchar(40),
mail varchar(30) );

vou dp voltar a inserir novamente o campo ID, e como é automatico é preciso fazer alguma coisa no codigo para adicionar autmaticament?

Um Abraço  🙂

SIGNIFICADOS: WWW: Wait,wait,wait... HTML: Hoje Ta Mais Lento.KEYBOARD: Dispositivo usado para entrada de erros no Computador.

Link to comment
Share on other sites

Usa o $_POST não uses variaveis globais...

//        global $nome;
// 	  global $mail;

        $nome = $_POST['nome'];
$mail = $_POST['mail'];

$nome = trim($nome);
$mail = trim($mail);


$connect = mysql_connect("localhost","admnews","adm") or die ("Erro a ligar a BD");
$insert = "INSERT into add_new values ('$nome','$mail')";
mysql_db_query("newsl", $insert, $connect);

header("Location: index.php");
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.