Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Submit & View Data

  1. #1
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Submit & View Data

    Greetings!

    I need simple php script, just submit data ( from html form ), the data goes
    stores into database. & that i can view from a page ( stored data ).

    Can any one give me this kind of thing.

    I shall be very thankful to him/her.


    K

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Below is the html interface through which the user insert the data and press submit button. Lets call this file insert1.html
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <form action="dbinsert.php" method="post" name="f1">
      <p>Name 
        <input name="nam" type="text" id="nam">
      </p>
      <p>Number 
        <input name="num" type="text" id="num">
      </p>
      <p>Message 
        <textarea name="com" cols="30" rows="5" id="com"></textarea>
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit">
      </p>
    </form>
    </body>
    </html>
    Below is the PHP script through which you can insert the entered data in the above mentioned file into the database. Lets call this file dbinsert.php

    PHP Code:
    <?php
    //filename: dbinsert.php
    // Set Mysql Variables

    $host 'localhost'
    $user 'root';
    $pass '';
    $db 'ex';
    $table 'comment'

    // Set global variables to easier names
    $name $_POST['nam']; 
    $number $_POST['num']; 
    $comment $_POST['com'];


    // Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
    mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db) or die(mysql_error());
    $add_all "INSERT INTO $table values('".$name."','".$number."','".$comment."')";
    mysql_query($add_all) or die(mysql_error());

    //echo "Record has been successfully inserted";

    echo "<script type=\"text/javascript\">location.href='viewdata.php';</script>";
    ?>
    The next is the script which will display the data from the database (Yuou've just entered). Lets call this file viewdata.php

    PHP Code:
    <?php
    class database
    {
        private 
    $db_handle;
        private 
    $user_name;
        private 
    $password;
        private 
    $data_base;
        private 
    $host_name;
        private 
    $sql;
        private 
    $results;

        function 
    __construct($host="localhost",$user,$passwd)
        {
            
    $this->db_handle mysql_connect($host,$user,$passwd);
        }

        function 
    dbSelect($db)
        {
            
    $this->data_base $db;
            if(!
    mysql_select_db($this->data_base$this->db_handle))
            {
                
    error_log(mysql_error(), 3"/phplog.err");
                die(
    "Error connecting to Database");
            }
        }
        
        function 
    executeSql($sql_stmt)
        {
            
    $this->sql $sql_stmt;
            
    $this->result mysql_query($this->sql);
        }
        function 
    outputGenerate()
        {
            echo 
    "<table cellspacing='0' cellpadding='0' width='900' border='1'>\n<tr>";
            echo 
    "<tr><td width=\"100\">Name</td><td width=\"100\">Number</td><td width=\"800\">Comments</td></tr><tr>";
            while(
    $record mysql_fetch_object($this->result))
            {
                echo 
    "<td width='100'>".$record->name."</td>";
                echo 
    "<td width='100'>".$record->number."</td>";
                echo 
    "<td width='800'>".$record->comment."</td>";
                echo 
    "</tr><tr><td colspan='3'>&nbsp;</td></tr><tr>";
            }
            echo 
    "</tr></table>";
        }
    }
    $host='';
    $user "root";
    $passwd "";
    $db "ex"
    $sql "SELECT * FROM COMMENT";
    $dbObject = new database($host,$user,$passwd);
    $dbObject->dbSelect($db);
    $dbObject->executeSql($sql);
    $dbObject->outputGenerate();
    ?>

    Hope you'll understand the script. The above mentioned items are just skelton s that does what you mentioned in your posting. You need to customized them according to your needs.

    Remember to edit the database related pages and specify your database and table names and if it is needed change the sql statements too in a way that it suitable for your needs.

    Hope this helps

  3. #3
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi!

    I've entered database name , db user & pass, now when i submit data, it
    gives the error : Table 'databasename.comment' doesn't exist

    How to create this ?

    K

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    If you look at the source code you can find out that the data we've stored in a table named COMMENT.

    If you have any other table name you should replace the table name that I've used in my source code to insert and retrieve data.

  5. #5
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Hi!

    Hello!

    I've not changed any thing in the files & except enter the database & host & db username & password.

    The table name is also same there.

    The error is asking about the table comment doesn't exist. How to create
    this table in the database?

    K

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Before running the following code please make sure that you have created a database in which the following code will put a table named comments based on which you can use the previous code snippets that I had provided.

    PHP Code:
    <?php
    class database
    {
        private 
    $db_handle;
        private 
    $user_name;
        private 
    $password;
        private 
    $data_base;
        private 
    $host_name;
        private 
    $sql;
        
        function 
    __construct($host="localhost",$user,$passwd)
        {
            
    $this->db_handle mysql_connect($host,$user,$passwd);
        }

        function 
    dbSelect($db)
        {
            
    $this->data_base $db;
            if(!
    mysql_select_db($this->data_base$this->db_handle))
            {
                
    error_log(mysql_error(), 3"/phplog.err");
                die(
    "Error connecting to Database");
            }
        }
        
        function 
    createTable($sql_stmt)
        {
            
    $this->sql $sql_stmt;
            
    mysql_query($this->sql) or die("Invalid Table Creation");
            
    $this->displayMessage();
        }
        
        function 
    displayMessage()
        {
            echo 
    "<script type='text/javascript'>alert('The table has been created successfully');</script>";
        }        
    }

    $user "root";
    $passwd "";
    $db "ex"
    $sql "CREATE TABLE comments (name varchar(50), number varchar(5), comment varchar(50));";

    $dbObject = new database($host,$user,$passwd);
    $dbObject->dbSelect($db);
    $dbObject->createTable($sql);

    ?>

  7. #7
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    HI!

    Sorry for replying late. I have run this file but it shows error. The main purpose of this file is that to run this :

    Code:
    $sql = "CREATE TABLE comments (name varchar(50), number varchar(5), comment varchar(50));";
    I've runned this query from phpMyAdmin. The table is created ...

    But Still when i view this file dbinsert.php
    It gives the error:
    'sitename_name.comment' doesn't exist

    * (db username & db name) names are same i.e sitename_name *

    $db = "ex"; <==== $db = "sitename_name";

    Im not understanding this error.

    K

  8. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    The code i've posted here works perfectly correct in my system.

    There is one small correction in my previous code i think you've used the following code in your PHPMyAdmin to create the table

    Code:
    Quote Originally Posted by codeexploiter
    $sql = "CREATE TABLE comments (name varchar(50), number varchar(5), comment varchar(50));";
    The above item must be the following, plz look at the table name

    Code:
    $sql = "CREATE TABLE comment (name varchar(50), number varchar(5), comment varchar(50));";
    I think you've created a table named comments not comment. Comment is the correct table name that I had used in my source code. That might be the problem here.

    I think it is a mistake from my side as I haven't checked it properly.

  9. #9
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Hi Back

    Hi!

    Sorry for replying late. ( I was out of city )

    Yes i've renamed it to :- Comment from Comments

    now when i store data in it, after storing it (dbinsert.php) ... it redirects to ..
    viewdata.php & give the following error:

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /viewdata.php on line 4


    Looking forward for reply

    Regards
    K

  10. #10
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi All!

    I've changed viewdata code with new code, & now database is showing its contents.

    To Get What i Need was Impossible without the help of codeexploiter
    Thank you Sir for helping me, B.C it was impossible without you thanks again.

    The New Viewdata code; with which database is showing its contents:

    <?

    $host = 'localhost';
    $username="";
    $password="";
    $database="";

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM comment";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    echo "<b><center>Database Output</center></b><br><br>";

    $i=0;
    while ($i < $num) {

    $name=mysql_result($result,$i,"name");
    $number=mysql_result($result,$i,"number");
    $comment=mysql_result($result,$i,"comment");


    echo "<table cellspacing='0' cellpadding='0' width='100&#37;' border='1'>\n<tr>";
    echo "<tr><td width=\"15%\">Name</td><td width=\"15%\">Number</td><td

    width=\"70%\">Comments</td></tr><tr>";

    {
    echo "<td width='15%'>".$name."</td>";
    echo "<td width='15%'>".$number."</td>";
    echo "<td width='70%'>".$comment."</td>";
    echo "</tr><tr><td colspan='3'>&nbsp;</td></tr><tr>";
    }
    echo "</tr></table>";


    $i++;
    }

    ?>
    Thanks once Again Sir ( codeexploiter ) !


    Seek new code help from:


    Thanks you ALL

    Best Regards!

    K
    Last edited by kaali; 01-27-2007 at 05:14 PM. Reason: Problem Solved Announcement!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •