tekkenlord wrote:Now my problem really is that I want to be able to create a data-entry form that would mean that I could easily input data into a html table. And I dont know how I would go abouts doing this. Does anyone know a tutorial where i can do this please thanks
Ive never helped anyone out on here because i normally need the help but you sound like me 2 weeks ago :p
First of all your going to need a MySQL database set up, if you dont have one google a tutorial on how to make one. The idea is to send information to database then use that information on request to output a html table.
Follow this for a tutorial on MySQL setup:
http://www.w3schools.com/PHP/php_mysql_connect.asp
As you create one you need to make a table call it table1, this should be in the tutorial, make the table have 2 columns for this example to work "name" and "favorite". You can make a new table and everything once you understand how the system works.
Once you have MySQL setup you can begin to make your form.
start the form like this
Code: Select all
<form action="http://[color=#FF0000]ENTERYOURWEBSITE[/color]/process.php" method="post">
This will tell what action the form takes when the user submits data.
Next is to create areas for users to input, in this example ill show you a text area and a one line area.
One line input:
This will have in my example the users name
<input name="name" type="text" value=" Enter Name Here" size="20" />
Text Area (multiple lines):
This will be favorite things in life (random i know)
Code: Select all
<textarea name="favorite" cols="20" rows="5" wrap="physical" >Write here what you love in life..isit php? I bet it is</textarea>
Obviously add as many or as little of each as you like play around with it. Also note the names of each input should be the same as the column in the table on MySQL. This makes it much easier.
Now when you got the form set out you need a submit button, this will send the data to the database. Here is an example:
Code: Select all
<input name="submit" type="submit" value="submit" />
So now all the users information is sent to process.php which is important, this is now a new php page.
Process.php
This will read the information and insert it into the database for you.
First of all the php script will connect to your database
Code: Select all
<?php
$host="localhost"; // Host name
$username="enter here"; // Mysql username
$password="enter here"; // Mysql password
$db_name="enter here"; // Database name
$tbl_name="enter here"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
Now you are connected to the database. Next up is inserting the data, carry on the code like this
Code: Select all
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, favorite)VALUES('$name', '$favorite')";
$result=mysql_query($sql);
Notice the two values used, the form input names. Change these to the form names you use.
Once inserted finish the script with this:
Code: Select all
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Thank you for submiting your job, our team will now take a look and approve very soon ";
echo "<BR>";
echo "<a href='http://miniworkers.justfree.com/jobs.php'>Click here to go back to miniworkers</a>";
}
else {
echo "ERROR";
}
?>
Now you have the form information into the database.
FINAL PART: createing the html table with the data... Start a new php page called table.php and start the script like this, connecting to the Database
Code: Select all
<?php
$host="localhost"; // Host name
$username="enter here"; // Mysql username
$password="enter here"; // Mysql password
$db_name="enter here"; // Database name
$tbl_name="enter here"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
Same as last time now we have a connection, next we tell the database what we want from the table in this case its names and favorites... so
Code: Select all
$result = mysql_query("SELECT `title`,`favorite` FROM `table1`") or die( mysql_error() );
This tells the database we want these two columns from table1.
Next its down to the table, lets start:
Code: Select all
// START AN OUTPUT TABLE
echo "<table class='sample5'>";
// IF NO RESULTS
if (!mysql_num_rows($result))
{
echo "<tr><td colspan='8'>The database has found no records</td></tr>";
}
// IF WE HAVE RESULTS
else
{
Now the script knows we want a table lets write the column titles:
Code: Select all
// TITLE FOR COLUMNS
echo "<tr><td nowrap><b>Name</b></td><td align='left'><b>favorite</b></th></tr>";
Obviously change them as needed when creating a new table for different information.
Next its getting the information we want into the table and ending the table like so:
Code: Select all
// ITERATE OVER THE RESULTS SET
while ($line = mysql_fetch_assoc($result))
{
// EASY-TO-READ LOCAL VARIABLES
foreach ($line as $key => $val) { $$key = htmlentities($val); }
// CREATE THE ROW OF DATA
echo "<tr>";
echo "<td>$name</td>\n";
echo "<td align='left'>$faovrite</td>\n";
} // END WHILE ITERATOR
echo "</table>\n";
} // END IF/ELSE
?>
And thats it there is a html table with the users inputed data from the form. Its like 1:30am and i dont know if thats clear but you will not get it all working first time, the trick is to not give up and if you get really stuck ask here.
LOL kinda went on a bit.... Ah well i might of got the wrong idea here but thats how i would do it.