← Desenvolvimento

erro em php

Lida 2443 vezes

Offline

Bruno Mota 
Membro
Mensagens 1733 Gostos 3
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

eu ando aqui a programar um script em php para noticias muito simples ora bem o meu erro
diz que insere na base de dados mas nao insere
aqui vai os cods
inserirnot.htm
Código: [Seleccione]
<form action="admnot.php" method="post" name="inser" id="inser">
<p style="margin-top: 0; margin-bottom: 0">
Adicionar noticia</p>
<p style="margin-top: 0; margin-bottom: 0">
Id da categoria:<br>
<font size="3" color="#808080">
<input name="id" type="text" id="tel0" size="30" maxlength="9"></font></p>
<p style="margin-top: 0; margin-bottom: 0">
Data:<br>
<font size="3" color="#808080">
<input name="data" type="text" id="tel1" size="30" maxlength="9"></font></p>
<p style="margin-top: 0; margin-bottom: 0">
Autor:<br>
<font size="3" color="#808080">
<input name="autor" type="text" id="tel2" size="30" maxlength="9"></font></p>
<p style="margin-top: 0; margin-bottom: 0">
Noticia:</p>
<p style="margin-top: 0; margin-bottom: 0">
<font color="#808080" size="1">
   <textarea name="noticia" cols="24" rows="8" id="msg" tabindex="0"></textarea></font><br>
<font size="3" color="#808080">
<input name="enviar" type="submit" id="enviar" value="OK"></font><font size="1"> </font>
<font size="3" color="#808080">
<input type="reset" value="Limpar"></font></p>
</form>


admnot.php
Citar
<?
include("mysql.php");
$id = $_POST['id'];
$noticia = $_POST['noticia'];
$data = $_POST['data'];
$autor = $_POST['autor'];

if(!empty($id)&& !empty($noticia)&& !empty($data)&& !empty($autor)){
$x = mysql_query("INSERT INTO noticias (id_cat, noticia, data, autor) VALUES ('$id','$noticia','$data','$autor')");
echo"assim grava";
}else{
echo"assim nao grava";
}  
?>

A tabela é assim

CREATE TABLE `cat_noticias` (
  `id` bigint(3) NOT NULL auto_increment,
  `nome` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `cat_noticias`
--

INSERT INTO `cat_noticias` VALUES (1, 'lol');
INSERT INTO `cat_noticias` VALUES (2, 'lol2');

-- --------------------------------------------------------

--
-- Table structure for table `noticias`
--

CREATE TABLE `noticias` (
  `id` bigint(10) NOT NULL auto_increment,
  `cat_id` bigint(3) NOT NULL default '0',
  `noticia` longtext NOT NULL,
  `data` varchar(10) NOT NULL default '',
  `autor` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `noticias`
--

INSERT INTO `noticias` VALUES (1, 1, 'lolanfkjdhfkjsahdfkjhsafkjhdsfkjshkjfdhsdkjfhdkjfhkjfhskjdfhskjdfhkjsdhfskjhdfkjsdhfkjshdfkjshfkjshdfkjshdfkshfdkjshdfkjdshfksdhf', '22/22/1000', 'bruno');

espero que me consigao ajudar
Offline

10igner 
Membro
Mensagens 717 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

também tive um problema semelhante, dizia que inseria tudo bem, mas na bd nada

acrescentei o código depois do query e funcionou, não me perguntes como, porque também não sei.

if (@mysql_query($sql)) {
    echo('<p>Anuncio adicionado com sucesso</p>');

}
Offline

kingless 
Membro
Mensagens 857 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2 Level 1

Código: [Seleccione]

<?
include("mysql.php");

$id = $_POST['id'];
$noticia = $_POST['noticia'];
$data = $_POST['data'];
$autor = $_POST['autor'];

if($id != "" AND $noticia != "" AND $data != "" AND $autor != ""){
$x = mysql_query("INSERT INTO noticias (id_cat, noticia, data, autor) VALUES ('$id','$noticia','$data','$autor')");
if(!$x) {
echo "Não gravou!";
} else {
echo "Gravou!";
}
?>


ERROS:

1º É preferivel utilizar-se  != ""  do que empty
2º O nome da coluna é cat_id e não id_cat
3º Para verificar se os dados foram inseridos tem que se colocar por exemplo:

if(!$query) {
echo "ERRO";
} else { echo "CERTO"; }

E não colocar simplesmente $x = mysql_query... echo "gravou";

Cumps   8)
Offline

OFFICER 
Membro
Mensagens 2076 Gostos 0
Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

Ele já tem isto bem..lolol só se esqueceu de postar que já tava :?
Offline

kingless 
Membro
Mensagens 857 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2 Level 1

MAs sempre ajuda  :wink:
Offline

OFFICER 
Membro
Mensagens 2076 Gostos 0
Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

Sim, ajuda a quem tenha o mesmo erro, lá isso é verdade ;)
Offline

Bruno Mota 
Membro
Mensagens 1733 Gostos 3
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

sim ja esta resolvido