Ir para conteúdo
Fórum Script Brasil
  • 0

Sistema de comentários com resposta


filipiarruda

Pergunta

5 respostass a esta questão

Posts Recomendados

  • 0

Está certo, o que eu tenho pronto até o momento está abaixo.

comentar.php

<?php require_once('Connections/vale_autos.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO comentarios (nome, email, mensagem) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['nome'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['mensagem'], "text"));

  mysql_select_db($database_vale_autos, $vale_autos);
  $Result1 = mysql_query($insertSQL, $vale_autos) or die(mysql_error());

  $insertGoTo = "comentado.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$maxRows_listar = 10;
$pageNum_listar = 0;
if (isset($_GET['pageNum_listar'])) {
  $pageNum_listar = $_GET['pageNum_listar'];
}
$startRow_listar = $pageNum_listar * $maxRows_listar;

mysql_select_db($database_vale_autos, $vale_autos);
$query_listar = "SELECT nome, email, mensagem FROM comentarios ORDER BY id DESC";
$query_limit_listar = sprintf("%s LIMIT %d, %d", $query_listar, $startRow_listar, $maxRows_listar);
$listar = mysql_query($query_limit_listar, $vale_autos) or die(mysql_error());
$row_listar = mysql_fetch_assoc($listar);

if (isset($_GET['totalRows_listar'])) {
  $totalRows_listar = $_GET['totalRows_listar'];
} else {
  $all_listar = mysql_query($query_listar);
  $totalRows_listar = mysql_num_rows($all_listar);
}
$totalPages_listar = ceil($totalRows_listar/$maxRows_listar)-1;
?>
<!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=utf-8" />
<link href="css-comentarios.css" type="text/css" rel="stylesheet" />
<title>Comentários</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="coment">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Nome:</td>
      <td><input type="text" name="nome" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Email:</td>
      <td><input type="text" name="email" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">Mensagem:</td>
      <td><textarea name="mensagem" cols="50" rows="5"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Enviar Comentário" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<table border="1" align="center" id="coment2">
  <tr>
    <td>Nome</td>
    <td>E-mail</td>
    <td>Mensagem</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_listar['nome']; ?></td>
      <td><?php echo $row_listar['email']; ?></td>
      <td><?php echo $row_listar['mensagem']; ?></td>
    </tr>
    <?php } while ($row_listar = mysql_fetch_assoc($listar)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($listar);
?>
atualizar.php
<?php require_once('Connections/vale_autos.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO comentarios (nome, email, mensagem) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['nome'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['mensagem'], "text"));

  mysql_select_db($database_vale_autos, $vale_autos);
  $Result1 = mysql_query($insertSQL, $vale_autos) or die(mysql_error());

  $insertGoTo = "comentado.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$maxRows_listar = 10;
$pageNum_listar = 0;
if (isset($_GET['pageNum_listar'])) {
  $pageNum_listar = $_GET['pageNum_listar'];
}
$startRow_listar = $pageNum_listar * $maxRows_listar;

mysql_select_db($database_vale_autos, $vale_autos);
$query_listar = "SELECT nome, email, mensagem FROM comentarios ORDER BY id DESC";
$query_limit_listar = sprintf("%s LIMIT %d, %d", $query_listar, $startRow_listar, $maxRows_listar);
$listar = mysql_query($query_limit_listar, $vale_autos) or die(mysql_error());
$row_listar = mysql_fetch_assoc($listar);

if (isset($_GET['totalRows_listar'])) {
  $totalRows_listar = $_GET['totalRows_listar'];
} else {
  $all_listar = mysql_query($query_listar);
  $totalRows_listar = mysql_num_rows($all_listar);
}
$totalPages_listar = ceil($totalRows_listar/$maxRows_listar)-1;
?>
<!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=utf-8" />
<link href="css-comentarios.css" type="text/css" rel="stylesheet" />
<title>Comentários</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="coment">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Nome:</td>
      <td><input type="text" name="nome" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Email:</td>
      <td><input type="text" name="email" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">Mensagem:</td>
      <td><textarea name="mensagem" cols="50" rows="5"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Enviar Comentário" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<table border="1" align="center" id="coment2">
  <tr>
    <td>Nome</td>
    <td>E-mail</td>
    <td>Mensagem</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_listar['nome']; ?></td>
      <td><?php echo $row_listar['email']; ?></td>
      <td><?php echo $row_listar['mensagem']; ?></td>
    </tr>
    <?php } while ($row_listar = mysql_fetch_assoc($listar)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($listar);
?>

Agradeço desde já a disposição em me ajudar, gostaria de poder colocar as respostas em uma tabela distinta no banco de dados.

Link para o comentário
Compartilhar em outros sites

  • 0

crie uma tabela no banco de dados chamada "comentarios" (por exemplo) e la crie os campos "id_comentario", "id_postagem", "nome", "email", "mensagem"

na página do comentario.php, quando inserido o comentario, é só passar por $_POST ou $_GET o id_postagem e os outros campos...

ai nessa mesma página, é só fazer um query selecionando os registros encontrados na tabela "comentarios" onde o "id_postagem" for igual ao id da postagem selecionada

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,7k
×
×
  • Criar Novo...