没有数据库选择错误


No database selected error

我把这个放在了网站的某个地方:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Comment system using php and mysql</title>
</head>
<body>
<?php
mysql_connect("localhost", "root", "")or die("cannot connect server ");
mysql_select_db("comments")or die("cannot select DB");
?>
<form name="comment" method="post" action="comment.php" onSubmit="return validation()">
<table width="500" border="0" cellspacing="3" cellpadding="3" style="margin:auto;">
  <tr>
    <td align="right" id="one">Name :<span style="color:#F00;">*</span></td>
    <td><input type="text" name="namename" id="tnameid"></td>
  </tr>
  <tr>
    <td align="right" id="one"></td>
    <td><textarea name="message" id="tmessageid"></textarea></td>
  </tr>
  <tr>
  <td align="right" id="one"></td>
  <td><input type="submit" name="submit" id="submit" value="Submit Comment"></td>
  </tr>
</table>
</form>
</body>
</html>

Comment.php如下所示:

<?php
if(isset($_POST['submit']))
{
 $name=$_POST['namename'];
 $message=$_POST['message'];
 $insert=mysql_query("insert into commenttable
                (name, message)values
                ('$name','$message')")or die(mysql_error());
 header("Location:index.php");
 }
?>

和我得到没有数据库选择时,我提交。我的数据库是"评论"与表"commenttable"answers"名称"answers"消息"里面。

你有两个程序

第一个连接到数据库服务器,从中选择一个数据库,然后向浏览器发送一个表单。

另一个尝试使用不存在的数据库连接。

您需要连接到您想要使用该连接的程序中的数据库。

如果你的HTML标记文件有扩展HTML <?php ?>将如何工作?

mysql_connectmysql_select_db放入comment.php文件