1

可能重复:
未选择数据库 - PHP & MySQL

我正在尝试使用 MySQL 数据库创建基本登录,但每次尝试登录时都会出现“未选择数据库”错误。我有一个包含两个表用户(登录名)和评论(帖子)的数据库设置。代码有点乱,不符合标准,但这不是供公众使用的。

这是代码:

<?php
error_reporting (0);
session_start();

if(!$_SESSION['username']){
    header ('location: login.php');
}
else{
error_reporting(0);
require('connect.php');
echo "<a href=\"logout.php\">Logout</a><br /><br /><br />";
$name = $_SESSION['username'];
$today = date("c");
//$today = date("c", strtotime()); 
$comment = $_POST['comment'];
$submit=isset($_POST['submit']);
if($submit)
{
    if($name&&$comment)
    {
    $query = mysql_query("INSERT INTO comment (name,comment,time) VALUES ('$name','$comment','$today')");
header("Location: success.php");
}
else
{
    echo "Please fill out all the fields.";
}}}
?>
<html>
<head>
<title>Comment Box</title>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function() {
  jQuery("abbr.timeago").timeago();
});
</script>
</head>
<style>
body {
background-color: #DFDFDF;
}
</style>
<body>
<link type="text/javascript" href="localtime.js" />
<form action="#" method="POST">
<label>Name: </label>
<br />
<input type="text" disabled="disabled" name="name" value="<?php echo "$name" ?>" />
<br />
<br />
<label>Comment: </label>
<br />
<textarea name="comment" cols="25" rows="7"></textarea>
<br />
<br />
<input type="submit" name="submit" value="Comment" />
<br />
</form>
<hr width="1100px" size="5px" />
</body>
<?php
require('connect.php');
$query=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($query))
{
 $id=$rows['id'];
 $dname=$rows['name'];
 $dcomment=$rows['comment'];
 $dtime=$rows['time'];
 $atime="<abbr class='timeago' title='$dtime'></abbr>";

 echo '<font color="red">Name:</font>  ' . $dname . '&nbsp; &nbsp;&nbsp;&nbsp;'. $atime      .     '<br />' . '<br />' . '<font color="red">Comments:</font>  ' . '<br />' . $dcomment . '&nbsp' . '&nbsp' .
      '&nbsp' . '&nbsp';

  if($_SESSION['username']=="admin"){
    echo "<a href=\"delete.php?id=" . $rows['id'] . "\">Delete User</a>"; 
 }
 else
 {
    echo "";
}


echo '<br />' . '<br />' . 
     '<hr size="5px" width="500px" color="blue" />' . '<br />' . '<br />' ;    
}
?>
</html>
4

1 回答 1

0

I don't know what you have on connect.php file. Maybe you forgot this line:

mysql_select_db("table_name",$db);
于 2012-09-27T17:18:13.393 回答