i want to make comment box in my own site so i have to make database online but it shows some error,
Please help me.!!
this is the code

<?php

      $comment = $_POST['comment'];
      $submit = $_POST['submit'];
      if($submit)
      {
           if($name&&$comment)
           {
                $con= mysql_connect("localhost", "root","");
                if($con)
               {
                    $query="create schema IF NOT EXISTS comments;";
                     mysql_select_db("comments");
                     $query="create table comment(id int NOT NULL AUTO_INCREAMENT, primary key(id),name varchar(50), comment varchar(300));";

                     $insert=mysql_query("insert into comment(name, comment) values('$_POST[name]', '$_POST[comment]')");
                     mysql_select_db("comments");
                      $query="select * from comment";
                     $res=mysql_query($query);
                      echo"<table>";
                       while($row=mysql_fetch_array($res))
                       {
                            echo "<tr>
                       <td><p></p></td>
                          <tr>";
                          echo "<tr>
                        <td><id>$row[id]</id></td>
                        <td><left>$row[name]</left></td>
                            <tr>";
                          echo "<tr>
                         <td></td>
                      <td><left1>$row[comment]</left1></td>
                        <td><p></p></td>
                             <tr>";
                               }
                             echo "</table>";
                       }
                             }
                       }
              ?>

Recommended Answers

All 4 Replies

Does the table comment exists? The complete error should be something like:

mysql_fetch_array() expects parameter 1 to be resource, boolean given

Which means the query on line 19 returned false. In addition from line 12 to 14 you're not executing any query. So if the table does not exists the other queries will generate errors.

Also:

The query string should not end with a semicolon.

Reference: http://php.net/manual/en/function.mysql-query.php

Once the problem is solved, I would suggest including something like:

$res=mysql_query($query);

if(!$res)
{
    // Do something to catch the error - maybe display
    // an error message, send the admin an error report
    // by email or try the query again
}

To provide a more useful error message if this recurs in future.

this is the complete code

<form method="post">
                                    <table>
                                    <tr>
                                            <td><input type="text" size="10" name="name" id="searchfield" title="Name"/>Required</td>
                                    </tr>
                                    <tr>
                                             <td><textarea type="text" size="10" name="comment"  id="searchfield1" title="Comment" style="font-family: Gill, Helvetica, sans-serif"></textarea>Required</td>
                                     </tr> 
                                     <tr>
                                         <td><input type="submit" value="Post" name="submit" style="background-color: #48ACDE;color: #000000;cursor:pointer;"/></td>
                                     </tr> 
                                  </table>
                                  </form>
                                <div id="comment">
                                <?php
                                require ('connect.php');
                                $name= $_POST['name'];
                                $comment = $_POST['comment'];
                                $submit = $_POST['submit'];
                                if($submit)
                                {
                                    if($name&&$comment)
                                    {
                                            $query="create schema if not exists comment;";
                                            mysql_select_db("comment");
                                            $r=mysql_query($query);
                                            if(!$r)
                                            {
                                                  die("can't create database".mysql_error());   
                                            }
                                            else{
                                                $query="create table comment(id int not null AUTO_INCREMENT,primary key(id) ,"."name varchar(20),submit text(200));";
                                            }
                                            $insert=mysql_query("insert into comment(name, comment) values('$_POST[name]', '$_POST[comment]')");
                                            mysql_select_db("comment");
                                            $query="select * from comment";
                                            $res=mysql_query($query);
                                            echo"<table>";
                                            while($row=mysql_fetch_array($res))
                                            {
                                                echo "<tr>
                                                    <td><p></p></td>
                                                  <tr>";
                                               echo "<tr>
                                                    <td><id>$row[id]</id></td>
                                                    <td><left>$row[name]</left></td>
                                                  <tr>";
                                               echo "<tr>
                                                    <td></td>
                                                    <td><left1>$row[comment]</left1></td>
                                               <td><p></p></td>
                                                  <tr>";
                                            }
                                            echo "</table>";
                                        }
                                        else
                                        {
                                            echo"<br/>";
                                            echo "Please fill out required field";
                                            $query="select * from comment";
                                            $res=mysql_query($query);
                                            echo"<table>";
                                            while($row=mysql_fetch_array($res))
                                            {
                                                echo "<tr>
                                                    <td><p></p></td>
                                                  <tr>";
                                               echo "<tr>
                                                    <td><id>$row[id]</id></td>
                                                    <td><left>$row[name]</left></td>
                                                  <tr>";
                                               echo "<tr>
                                                    <td></td>
                                                    <td><left1>$row[comment]</left1></td>
                                               <td><p></p></td>
                                                  <tr>";
                                            }
                                            echo "</table>";
                                        }
                                    }
                                    else
                                    {
                                        echo"<br/>";
                                        echo "Please fill out required field";
                                        $query="select * from comment";
                                            $res=mysql_query($query);
                                            echo"<table>";
                                            while($row=mysql_fetch_array($res))
                                            {
                                                echo "<tr>
                                                    <td><p></p></td>
                                                  <tr>";
                                               echo "<tr>
                                                    <td><id>$row[id]</id></td>
                                                    <td><left>$row[name]</left></td>
                                                  <tr>";
                                               echo "<tr>
                                                    <td></td>
                                                    <td><left1>$row[comment]</left1></td>
                                               <td><p></p></td>
                                                  <tr>";
                                            }
                                            echo "</table>";
                                    }
                                    ?>
                          </div>

I would suggest another code structure:

Part 1 is the part where you create the table. You do not have to execute this every time the page is loaded, or even check if this needs to be executed; just create the table once, then remove the code.

Part 2 is the part where you check if any data is posted to the page. If it is, insert it into your database.

Part 3 is the part where you retrieve all comments from your database. Seemingly this part can be executed every time the page is loaded, so you do not necessarily have to put this code inside an if-else statement.

About your query problem: has it been fixed yet?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.