-1

我正在创建具有评论系统的社交媒体网站,其中用户编写的文本将发布在板上并存储在数据库中,但问题是没有显示文本,也没有文本存储在数据库中,但旁边的其他字段文本已存储,因此任何人都可以帮助我???

配置文件.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(); ?>
4

2 回答 2

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

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

除此之外,您应该考虑切换到mysqlior PDO,因为mysql_*现在不推荐使用函数。

于 2013-03-09T15:06:20.490 回答
0

我在您的代码中看到了多个问题。首先,$writenCom = (isset($_POST['post']));设置$writenCom为 TRUE/FALSE,这是您尝试在 DB 中插入的值。其次,仅当您执行查询mysql_fetch_array时才有意义。SELECT此外,您应该始终转义来自用户的数据(建议使用mysqlior PDO,而不是 obsolete mysql

于 2013-03-09T15:05:32.697 回答