Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa Noite pessoal, estou com um problema na parte de cadastro e edição de noticias, As vezes eu consigo cadastrar, mais percebi que depende do texto, dei uma procurada a respeito mais não consegui resultados.A mensagem de erro é a seguinte:
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 'categoria = '', data = '2011-06-26 04:28:16', valor_real = '30,75' valor_pagsegu' at line 1
Eu já revisei várias vezes, e não consegui ajeitar.
-- Scripts --
Cadastro:
<?php if(isset($_POST['cadastrar_post']) && $_POST['cadastrar_post'] == 'cad'){
$usuario = $_SESSION['MM_Username'];
$pega_autor = mysql_query("SELECT id FROM up_users WHERE usuario = '$usuario'")
or die(mysql_error());
if(@mysql_num_rows($pega_autor) <= '0') echo 'Erro ao lesecionar o usuario';
else{
while($res_autor=mysql_fetch_array($pega_autor)){
$id_autor = $res_autor[0];
$img = $_FILES['thumb'];
$titulo = $_POST['titulo'];
$texto = $_POST['texto'];
$categoria = $_POST['categoria'];
$data = $_POST['data'];
$autor = "$id_autor";
$valor_real = $_POST['valor_real'];
$valor_pag = $_POST['valor_pagseguro'];
$pasta = "../uploads/$categoria";
$permitido = array('image/jpg','image/jpeg', 'image/pjpeg');
require("scripts/funcao_upload.php");
$nome = $img['name'];
$tmp = $img['tmp_name'];
$type = $img['type'];
$entrada = trim("$data");
if(strstr($entrada, "/")){
$aux = explode("/", $entrada);
$aux2 = date('H:i:s');
$aux3 = $aux[2] . "-" . $aux[1] . "-" . $aux[0] . " " . $aux2;
}
if(!empty($nome) && in_array($type, $permitido)){
$name = md5(uniqid(rand(), true)).".jpg";
Redimensionar($tmp, $name, 500, $pasta);
$cadastrar_noticias = mysql_query("INSERT INTO up_post (thumb, titulo, texto, categoria, data, autor, valor_real, valor_pagseguro, visitas)
VALUES ('$name', '$titulo', '$texto', '$categoria', '$aux3', '$autor', '$valor_real', '$valor_pag', '1')")
or die(mysql_error());
if($cadastrar_noticias >= '1'){
echo "<div class=\"ok\">Seu tópico foi cadastrado com sucesso!</div>";
}else{
echo "<div class=\"no\">Erro ao cadastrar o tópico</div>";
}
}
}
}
}
?>
<form name="cadastrar_posts" id="cadastrar_posts" method="post" action="" enctype="multipart/form-data">
<fieldset>
<label>
<span>Imagem de exibição</span>
<input type="file" name="thumb" class="thumb" size="30" />
</label>
<label>
<span>Titulo</span>
<input type="text" name="titulo" />
</label>
<label>
<span>Texto</span>
<textarea name="texto" rows="5"></textarea>
</label>
<label>
<span>Categoria</span>
<select name="categoria" id="categoria">
<option value="">Selecione a Categoria</option>
<option value="novidades" id="novidades">Novidades</option>
<option value="cursos" id="cursos">Cursos</option>
<option value="produtos" id="produtos">Produtos</option>
</select>
</label>
<label>
<span>Data</span>
<input type="text" name="data" />
</label>
<div id="produtos_class" style="display:none;"> <br />
<div class="no"><strong>Para postar um produto, os campos abaixo são obrigatórios</strong></div>
<label>
<span>Valor R$ <strong>(EX: 15,50)</strong></span>
<input type="text" name="valor_real" />
</label>
<label>
<span>Valor PagSeguro <strong>(EX: 1550)</strong></span>
<input type="text" name="valor_pagseguro" />
</label>
</div> <!-- DIV CLASS -->
<input type="hidden" name="cadastrar_post" value="cad" />
<input type="submit" value="Cadastrar" name="Cadastrar" class="cadastro_btn" />
</fieldset>
</form>
Edição:
<?php if(isset($_POST['cadastrar_post']) && $_POST['cadastrar_post'] == 'cad'){
$usuario = $_SESSION['MM_Username'];
$pega_autor = mysql_query("SELECT id FROM up_users WHERE usuario = '$usuario'")
or die(mysql_error());
if(@mysql_num_rows($pega_autor) <= '0') echo 'Erro ao lesecionar o usuario';
else{
while($res_autor=mysql_fetch_array($pega_autor)){
$id_autor = $res_autor[0];
$id_a_editar = $_POST['id_do_post'];
$img = $_FILES['thumb'];
$titulo = $_POST['titulo'];
$texto = $_POST['texto'];
$categoria = $_POST['categoria'];
$data = $_POST['data'];
$autor = "$id_autor";
$valor_real = $_POST['valor_real'];
$valor_pag = $_POST['valor_pagseguro'];
$pasta = "../uploads/$categoria";
$permitido = array('image/jpg','image/jpeg', 'image/pjpeg');
require("scripts/funcao_upload.php");
$nome = $img['name'];
$tmp = $img['tmp_name'];
$type = $img['type'];
$entrada = trim("$data");
if(strstr($entrada, "/")){
$aux = explode("/", $entrada);
$aux2 = date('H:i:s');
$aux3 = $aux[2] . "-" . $aux[1] . "-" . $aux[0] . " " . $aux2;
}
if (empty($_FILES['thumb']['name'])){
$editar_post = mysql_query("UPDATE up_post SET titulo = '$titulo', texto = '$texto' categoria = '$categoria', data = '$aux3', valor_real = '$valor_real' valor_pagseguro = '$valor_pag' WHERE id = '$id_a_editar'")
or die (mysql_error());
}else{
if(!empty($nome) && in_array($type, $permitido)){
$name = md5(uniqid(rand(), true)).".jpg";
Redimensionar($tmp, $name, 500, $pasta);
$cadastrar_noticias = mysql_query("INSERT INTO up_post (
thumb,
titulo,
texto,
categoria,
data,
autor,
valor_real,
valor_pagseguro,
visitas)
VALUES ('$name',
'$titulo',
'$texto',
'$categoria',
'$aux3',
'$autor',
'$valor_real',
'$valor_pag',
'1')")
or die(mysql_error());
if($cadastrar_noticias >= '1'){
echo "<div class=\"ok\">Seu tópico foi cadastrado com sucesso!</div>";
}else{
echo "<div class=\"no\">Erro ao cadastrar o tópico!</div>";
}
}
}
}
}
}
?>
<?php
$editar_post_id = $_POST['id_do_post'];
$noticias = mysql_query("SELECT
id,
thumb,
titulo,
texto,
categoria,
`data`,
autor,
valor_real,
valor_pagseguro,
visitas
FROM up_post
WHERE id = '$editar_post_id'")
or die(mysql_error()); echo "<strong>não encontramos notícias neste momento</strong><br /><br />";while($res_noticias=mysql_fetch_array($noticias)){
$id_do_post = $res_noticias[0];
$thumb = $res_noticias[1];
$titulo = $res_noticias[2];
$texto = $res_noticias[3];
$categoria = $res_noticias[4];
$data = $res_noticias[5];
$autor = $res_noticias[6];
$valor_real = $res_noticias[7];
$valor_pagseguro = $res_noticias[8];
$visitas = $res_noticias[9];
?>
<form name="editar_posts" id="editar_posts" method="post" action="" enctype="multipart/form-data">
<fieldset>
<label>
<span>Imagem de exibição <strong>(Somente necessário se quiser alterá-la)</strong></span>
<input type="file" name="thumb" class="thumb" size="30" />
</label>
<label>
<span>Titulo</span>
<input type="text" name="titulo" value="<?php echo $titulo; ?>"/>
</label>
<label>
<span>Texto</span>
<textarea name="texto" rows="5"><?php echo $texto; ?></textarea>
</label>
<label>
<span>Categoria</span>
<select name="categoria" id="categoria">
<option value=""><?php echo $categoria; ?></option>
<option value="novidades" id="novidades">Novidades</option>
<option value="cursos" id="cursos">Cursos</option>
<option value="produtos" id="produtos">Produtos</option>
</select>
</label>
<label>
<span>Data</span>
<input type="text" name="data" value="<?php echo date('d/m/Y', strtotime($data)); ?>"/>
</label>
<?php if ($categoria == 'produtos'){
echo "<div id=\"produtos_class\">";
}else{
echo "<div id=\"produtos_class\" style=\"display:none;\">";
}
?>
<label>
<span>Valor R$ <strong>(EX: 15,50)</strong></span>
<input type="text" name="valor_real" value="<?php echo $valor_real; ?>"/>
</label>
<label>
<span>Valor PagSeguro <strong>(EX: 1550)</strong></span>
<input type="text" name="valor_pagseguro" value="<?php echo $valor_pagseguro; ?>"/>
</label>
</div> <!-- DIV CLASS -->
<input type="hidden" name="id_do_post" value="<?php echo $id_do_post;?>" />
<input type="hidden" name="cadastrar_post" value="cad" />
<input type="submit" value="Editar" name="Editar" class="cadastro_btn" />
</fieldset>
</form>
<?php
}
}
?>
Agradeço a todos desde já!
Cara o erro é no SQL, verifica se seus campos na tabela são todos varchar como você colocou, pq você colocou aspas ate nos campos que parecem que vão guardar números, por exemplo: valor_real, valor_pagamento, se são números não podem ter aspas.
Abs
Na tabela estavam todos em "Texto". Daí exclui tudo e mudei para VarChar. Mais aparece isso aqui quando tento cadastrar:
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 's standard dummy text ever since the 1500s, when an unknown printer took a galle' at line 2.
Alguma idéia?
É isso mesmo que acabei de falar, você nao pode colocar todos os campos como varchar. Os campos so serao varchar quando forem strings, quando forem números terão que ser int e por ai vai.
Abs
Hmmm..Eu criei a tabela no Navicat,aqui ela,
Nunca mexi com sites antes, é o 1º que estou desenvolvendo...
CREATE TABLE `up_post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`thumb` text,
`titulo` text,
`texto` text,
`categoria` text,
`data` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`autor` text,
`valor_real` varchar(0) DEFAULT NULL,
`valor_pagseguro` varchar(0) DEFAULT NULL,
`visitas` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=latin1;Certo, eu colocaria no campo data=>Date ou DateTime, no valor_real=>float ou Decimal, o mesmo para valor_pagseguro se for guardar numeros, visitas se for guardar numero tem que ser int ou float.
Uma dúvida categoria vai guardar o q? strings ou números?
Abs
São 3 Categorias:
Produtos, Noticias e Cursos
Eu tentei cadastrar este texto:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Não deu certo,
Daí tentei esse aqui:
aslkdlaksmdlçaksmdlamsdadflakmcçlkmaçslcaisjaijfasifkmxcvlkznvaldsnmfaasfljdaf
Daí funcionou!
Opa, acho que descobri o problema...O texto é em Inglês e MYSQL ta reconhecendo como parte da Query. Como resolvo isso?
Resolvi uma parte, adicionei "addslashes" na variável texto,
Agora o problema está na parte de edição, quando eu altero algo e click em enviar, ele me retorna a seguinte mensagem:
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 'categoria = '', data = '2011-06-05 05:03:41', valor_real = '50,25' valor_pagsegu' at line 1
<?php include"scripts/restrict_no.php" ;?>
<?php include"header.php" ;?>
<div id="box">
<div id="header">
</div> <!-- HEADER -->
<div id="content">
<div id="menu">
<?php include"menu.php"; ?>
</div> <!--MENU-->
<div id="conteudo">
<span class="caminho">Home » Editar Posts</span>
<h1>Cadastrar Posts</h1>
<?php if(isset($_POST['cadastrar_post']) && $_POST['cadastrar_post'] == 'cad'){
$usuario = $_SESSION['MM_Username'];
$pega_autor = mysql_query("SELECT id FROM up_users WHERE usuario = '$usuario'")
or die(mysql_error());
if(@mysql_num_rows($pega_autor) <= '0') echo 'Erro ao lesecionar o usuario';
else{
while($res_autor=mysql_fetch_array($pega_autor)){
$id_autor = $res_autor[0];
$id_a_editar = $_POST['id_do_post'];
$img = $_FILES['thumb'];
$titulo = $_POST['titulo'];
$texto = $_POST['texto'];
$categoria = $_POST['categoria'];
$data = $_POST['data'];
$autor = "$id_autor";
$valor_real = $_POST['valor_real'];
$valor_pag = $_POST['valor_pagseguro'];
$pasta = "../uploads/$categoria";
$permitido = array('image/jpg','image/jpeg', 'image/pjpeg');
require("scripts/funcao_upload.php");
$nome = $img['name'];
$tmp = $img['tmp_name'];
$type = $img['type'];
$entrada = trim("$data");
if(strstr($entrada, "/")){
$aux = explode("/", $entrada);
$aux2 = date('H:i:s');
$aux3 = $aux[2] . "-" . $aux[1] . "-" . $aux[0] . " " . $aux2;
}
if (empty($_FILES['thumb']['name'])){
$editar_post = mysql_query("UPDATE up_post SET titulo = '$titulo', texto = '$texto' categoria = '$categoria', data = '$aux3', valor_real = '$valor_real' valor_pagseguro = '$valor_pag' WHERE id = '$id_a_editar'")
or die (mysql_error());
}else{
if(!empty($nome) && in_array($type, $permitido)){
$name = md5(uniqid(rand(), true)).".jpg";
Redimensionar($tmp, $name, 500, $pasta);
$cadastrar_noticias = mysql_query("INSERT INTO up_post (
thumb,
titulo,
texto,
categoria,
data,
autor,
valor_real,
valor_pagseguro,
visitas)
VALUES ('$name',
'$titulo',
'$texto',
'$categoria',
'$aux3',
'$autor',
'$valor_real',
'$valor_pag',
'1')")
or die(mysql_error());
if($cadastrar_noticias >= '1'){
echo "<div class=\"ok\">Seu tópico foi cadastrado com sucesso!</div>";
}else{
echo "<div class=\"no\">Erro ao cadastrar o tópico!</div>";
}
}
}
}
}
}
?>
<?php
$editar_post_id = $_POST['id_do_post'];
$noticias = mysql_query("SELECT
id,
thumb,
titulo,
texto,
categoria,
`data`,
autor,
valor_real,
valor_pagseguro,
visitas
FROM up_post
WHERE id = '$editar_post_id'")
or die(mysql_error()); echo "<strong>não encontramos notícias neste momento</strong><br /><br />";while($res_noticias=mysql_fetch_array($noticias)){
$id_do_post = $res_noticias[0];
$thumb = $res_noticias[1];
$titulo = $res_noticias[2];
$texto = $res_noticias[3];
$categoria = $res_noticias[4];
$data = $res_noticias[5];
$autor = $res_noticias[6];
$valor_real = $res_noticias[7];
$valor_pagseguro = $res_noticias[8];
$visitas = $res_noticias[9];
?>
<form name="editar_posts" id="editar_posts" method="post" action="" enctype="multipart/form-data">
<fieldset>
<label>
<span>Imagem de exibição <strong>(Somente necessário se quiser alterá-la)</strong></span>
<input type="file" name="thumb" class="thumb" size="30" />
</label>
<label>
<span>Titulo</span>
<input type="text" name="titulo" value="<?php echo $titulo; ?>"/>
</label>
<label>
<span>Texto</span>
<textarea name="texto" rows="5"><?php echo $texto; ?></textarea>
</label>
<label>
<span>Categoria</span>
<select name="categoria" id="categoria">
<option value=""><?php echo $categoria; ?></option>
<option value="novidades" id="novidades">Novidades</option>
<option value="cursos" id="cursos">Cursos</option>
<option value="produtos" id="produtos">Produtos</option>
</select>
</label>
<label>
<span>Data</span>
<input type="text" name="data" value="<?php echo date('d/m/Y', strtotime($data)); ?>"/>
</label>
<?php if ($categoria == 'produtos'){
echo "<div id=\"produtos_class\">";
}else{
echo "<div id=\"produtos_class\" style=\"display:none;\">";
}
?>
<label>
<span>Valor R$ <strong>(EX: 15,50)</strong></span>
<input type="text" name="valor_real" value="<?php echo $valor_real; ?>"/>
</label>
<label>
<span>Valor PagSeguro <strong>(EX: 1550)</strong></span>
<input type="text" name="valor_pagseguro" value="<?php echo $valor_pagseguro; ?>"/>
</label>
</div> <!-- DIV CLASS -->
<input type="hidden" name="id_do_post" value="<?php echo $id_do_post;?>" />
<input type="hidden" name="cadastrar_post" value="cad" />
<input type="submit" value="Editar" name="Editar" class="cadastro_btn" />
</fieldset>
</form>
<?php
}
}
?>
</div> <!--CONTEUDO-->
</div> <!--CONTENT-->
<div id="clear"></div>
</div> <!-- BOX -->
<?php include"footer.php";?>Ainda é o mesmo erro que te falei, você está inserindo valores que são float ou int como varchar.
Abs
Verifique essa linha
UPDATE up_post SET titulo = '$titulo', texto = '$texto' categoria = '$categoria', data = '$aux3', valor_real = '$valor_real' valor_pagseguro = '$valor_pag' WHERE id = '$id_a_editar'
Está faltando uma virgula entre o texto e a categoria e entre a data e o valor real
deveria ser
UPDATE up_post SET titulo = '$titulo', texto = '$texto', categoria = '$categoria', data = '$aux3', valor_real = '$valor_real', valor_pagseguro = '$valor_pag' WHERE id = '$id_a_editar'
<?php if(isset($_POST['cadastrar_post']) && $_POST['cadastrar_post'] == 'cad'){
'cad' é uma varável???
ñ deveria ser '$cad' ???
>
Ainda é o mesmo erro que te falei, você está inserindo valores que são float ou int como varchar.
Abs
Mais não tem nenhum como Varchar!
Você tentou alterar o update inserindo as virgulas?
UPDATE up_post SET titulo = '$titulo', texto = '$texto', categoria = '$categoria', data = '$aux3', valor_real = '$valor_real', valor_pagseguro = '$valor_pag' WHERE id = '$id_a_editar'Consegui resolver o problema...Agradeço a ajuda de todos!
Acho que seria tratamento de variáveis no insert, essa dúvida se encaixa melhor em PHP
Movido de Dreamweaver :seta: PHP