2013-03-09 127 views
-1

我正在创建一个评论系统的社交媒体网站,其中用户写的文本将发布在论坛上并存储在数据库中,但问题是没有文本显示,也没有文本存储在数据库中,但文本旁边的其他领域存储,所以任何人都可以帮助我?php mysql +评论系统

轮廓.PHP

<?php 
ob_start(); 
session_start(); 
require_once('for members/scripts/global.php'); 

if($_SESSION['login'] != 'true'){ 
     header("location:index.php"); 
    } 
$user_id = $_SESSION['user_id']; 
$send =(isset($_POST['send'])); 
$writenCom = (isset($_POST['post'])); 
if($send && $writenCom){ 
    echo $writenCom; 
$query = mysql_query("INSERT INTO comment(sender_id, text, comment_date)VALUES('$user_id', '$writenCom', now())")or die(mysql_error()); 

while($row = mysql_fetch_array($query)){ 

echo"comment success"; 


} 


} 

?> 

<!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="style/stylesheet.css"rel="stylesheet" type="text/css"/> 
<title> 
<?php print($_SESSION['first_name']); ?> 
<?php print($_SESSION['last_name']); ?>'s profile</title> 
<style type="text/css"> 

</style> 



<link href="style/stylesheet.css" type="text/css"/> 
</head> 

<body> 
<?php require_once('header.php'); ?> 

<div class="container center"> 
<div class="postForm"> 
<form action="<?php echo($_SESSION['first_name']); ?>" method="post"> 
    <textarea id="post" name="post" rows="5" cols="70"> </textarea> 
    <input type="submit" name="send" value="Post" style="background-color:#DCE5EE; float:right; border:1px solid #666; color:#666; height:73px; width:65px;" /> 
</form> 

</div> 


<div class="profilePost">Your Post will go here... 

</div> 
<!--for posting area --> 
<div class="textProfileHeader"><?php echo($_SESSION['first_name']); ?>'s profile</div> 


<!--end of posting --> 
<div class="profileImage"><img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="250" width="200" alt="<?php echo($_SESSION['first_name']); ?>'s profile" title="="<?php echo($_SESSION['first_name']);?>'s profile /></div> 

<div class="profiletextContent">Some Content about this person profile...</div> 

<div class="textProfileHeaderFriends"><?php echo($_SESSION['first_name']); ?>'s Friends</div> 

<div class="profileImgFriends"> <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; <img src="image with highlight 2/images/new images/imageHover/homeHover.jpg" height="50" width="40" />&nbsp;&nbsp; </div> 

<!-- 
<form id="form" method="post" action="profile.php" enctype="multipart/form-data" target="iframe"> 
    <input type="file" id="file" name="file" /> 
    <input type="submit" name="submit" id="submit" value="Upload File" /> 
</form> 
--> 
<!-- 
<a href="#">edit profile</a><br /> 
<a href="#">account settings</a><br /> 
--> 
<?php 
//}else{ 
    //header("Location: home.php"); 
?> 
<!-- 
<a href="#">private message</a><br /> 
<a href="#">add as friend</a><br /> 
--> 
<?php 
//} 
?> 
</div> 

<p>&nbsp;</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
<?php require_once('footer.php'); ?> 

</body> 
</html> 

<?php flush(); ?> 
+0

如果您只显示代码的相关部分,您可以更轻松地帮助您......关于您的问题:'$ writenCom =(isset($ _ POST ['post']));'指定一个布尔值为'$ writenCom'。你不想知道它是否设置了,你想要它的值(并且你想先清理它):'''writenCom = mysql_real_escape_string($ _ POST ['post']);' - 但是请不要使用'mysql_ *' - 改用'mysqli_ *'或PDO。 – Quasdunk 2013-03-09 15:01:42

+0

您正在'INSERT'查询中调用'mysql_fetch_array()'。这将无法工作。 INSERT查询返回true/false,所以你会'if($ query){//它工作...}'。你也不是逃避你的输入,所以这是开放的注射。至少你必须在所有'$ _POST'查询输入中调用'mysql_real_escape_string()'。 – 2013-03-09 15:04:20

+0

创建一个函数,根据用户ID获取用户数据 – samayo 2013-03-09 15:05:38

回答

0

我看到你的代码的多个问题。首先,$writenCom = (isset($_POST['post']));$writenCom设置为TRUE/FALSE,这是您尝试在数据库中插入的值。其次,只有在执行SELECT查询时,mysql_fetch_array才有意义。此外,你应该总是逃脱它来自用户的数据(它建议使用mysqliPDO,没有过时mysql

+0

“这是建议使用mysqli或PDO,而不是过时的mysql”是完全不相关的问题。 – bizzehdee 2013-03-09 16:10:40

+0

@bizzehdee:你没有看到它以“另外”开头吗?我概述了现有代码的问题,并添加了关于转义变量的常见注释 – a1ex07 2013-03-09 16:39:09

1

您应该使用mysql_affected_rows,而不是mysql_fetch_array

if (mysql_affected_rows() > 0) { 
    echo "comment success"; 
} else { 
    echo "insert comment failed"; 
} 

mysql_fetch_array是用于获取结果集select查询。

或者,你可以只是测试的mysql_query的返回值,这要么是TRUE成功或FALSE失败的INSERT语句

if ($query) { 
    echo "comment success"; 
} else { 
    echo "insert comment failed"; 
} 

除此之外,你应该考虑切换到任何mysqliPDO,因为现在已弃用mysql_*函数。