PDA

View Full Version : Submit & View Data



kaali
10-29-2006, 12:48 PM
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

codeexploiter
10-31-2006, 09:20 AM
Below is the html interface through which the user insert the data and press submit button. Lets call this file insert1.html


<!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
//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
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

kaali
11-08-2006, 03:56 PM
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

codeexploiter
11-09-2006, 02:45 AM
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.

kaali
11-12-2006, 07:37 AM
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

codeexploiter
11-13-2006, 08:54 AM
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
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);

?>

kaali
11-18-2006, 02:47 PM
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 :


$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

codeexploiter
11-21-2006, 10:37 AM
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



$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


$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.

kaali
12-30-2006, 11:28 AM
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

kaali
01-18-2007, 11:49 AM
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:


http://www.siteground.com/tutorials/php-mysql/display_table_data.htm


Thanks you ALL

Best Regards!

K

codeexploiter
02-06-2007, 07:36 AM
Hi Kaali

Its nice to hear that you've figured the problem.

regards

futonguy
10-27-2009, 06:03 AM
Hi,

I am currently writing a script quite simliar to this thread.
However I had my 'submit' code written and post to another php file. Now all i need is a code that i can write to view my table. the script for my table had already been written and save into another php file.

Is there a code whereby i can write together with my submit form to 'link' it to my table php file?

Form name is : testform.php
Processing form is : testprocess.php
table form : table.php

under my form file, i had written:


<form action="testprocess.php" method="post">

Thanks for the help.

futonguy
10-27-2009, 06:15 AM
Hey guys, no worries about my previous post..
already solved that problem.

cheers :):)

Just incase if anyone comes across this thread, here's the solution.



header("Location: table.php")