0

I included this somewhere on the website:

<!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 looks like tihs:

<?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");
 }
?>

and I get No database selected when I submit. My database is "comments" with a table "commenttable" and "name" and "message" inside it.

user3604398
  • 31
  • 1
  • 1
  • 3
  • 3
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 07 '14 at 12:31
  • you are not selecting any database using connection parameter ? – user3470953 May 07 '14 at 12:31
  • Do not use `mysql_*`: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Felix May 07 '14 at 12:32
  • [Don't use layout tables](https://www.google.co.uk/search?q=css+forms) and please [learn to love labels](http://www.456bereastreet.com/archive/200711/use_the_label_element_to_make_your_html_forms_accessible/) – Quentin May 07 '14 at 12:33
  • 1
    put your `mysql_connect` and `mysql_select_db` functions in `comment.php` file. – shyammakwana.me May 07 '14 at 12:38

2 Answers2

0

You have two programs.

The first one connects to a database server, selects a database from it, then sends a form to the browser.

The other one tries to use a database connection that doesn't exist.

You need to connect to the database in the program where you want to use that connection.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

If your HTML markup file has extension HTML how <?php ?> is gonna work ?

Put your mysql_connect and mysql_select_db in comment.php file.

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50