Edit a news page

Asked

Viewed 1,371 times

-4

In the code below, I have the news editor.

<?php

/*
 * faz a conexao ao banco
 * e seleciona a base de dados
 */
include ('../conn/conn.php');
/*
 * monta e executa consulta em SQL
 */
$sql = "SELECT *  FROM noticias   WHERE    n_id = ".(int)$_GET['id'];

$resultado = mysql_query($sql)
or die ("Não foi possível realizar a consulta.");

$linha = mysql_fetch_array($resultado, MYSQL_ASSOC);

?>

<h1>Alterar Noticia</h1>

<form action="alterar_noticia.php?id=<?php echo $_GET['id'] ?>" method="post">

  <label for="titulo">Titulo do Texto: </label>
  <input name="titulo" id="n_titulo" type="text" 
  value="<?php echo $linha['n_titulo'] ?>" /><br />

  <label for="texto">Texto: </label>
  <textarea name="texto" id="n_texto" rows="10" cols="30" /> 
  <?php echo $linha['n_texto'] ?></textarea><br />


  <input type="submit" value="Alterar" />


</form>

But when I click the button alter, redirects to another page. Where I have this code:

<?php

/*
 * faz a conexao ao banco
 * e seleciona a base de dados
 */
include '../conn/conn.php';
/*
 * monta e executa consulta em SQL
 */
$sql = "UPDATE 
        noticias 
    SET 
        titulo='".mysql_real_escape_string($_POST['titulo'])."', 
        texto='".mysql_real_escape_string($_POST['texto'])."', 
    WHERE 
         n_id = ".(int)$_GET['id'];

$resultado = mysql_query($sql)
or die ("Erro ao alterar notícia.");

?>

<h1>Notícia alterada com sucesso!</h1>

In this last code, errors appear.

  • 3

    we can help. But when looking at this question it is very difficult to perceive anything... Try to work it

  • What errors appear?

  • that answer they put helped me but agr appears me this

  • Unknown column title in field list?

1 answer

2

It appears to be syntax error only, there’s a comma left before the where, remove her.

$sql = "UPDATE noticias SET 
        titulo='".mysql_real_escape_string($_POST['titulo'])."', 
        texto='".mysql_real_escape_string($_POST['texto'])."', 
     --------------------------------------------------------^
         WHERE n_id = ".(int)$_GET['id'];

Avoid using mysql_* functions they are obsolete, in this other question, has several reasons why nay use them.

Do not use generic error messages like :

or die ("Error changing news.");

prefira mysql_error() this will give the bank error message:

 or die (mysql_error());
  • I did what I said and agr shows me this. Unknown column title in field list ?

  • 1

    This error may be caused by not having a column called titulo or a missing quote. When you run a echo $sql Is there a missing quote? if you can put the echo output in the comment.

  • thanks for the help

  • managed to solve? what was the problem?

  • was the table name wasn’t right

  • @Delfino, thanks for the corrections :D. For most languages do not need to add <!-- language: php --> Highlight is defined by the language tag, in a few cases it doesn’t happen automatically, tbm is useful if you have a response with several languages separated by block.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.