New to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

New to php

Post by tekkenlord »

Ok, so i'm new to this whole php scripting thing. I am making a site, with a friend who knows no php so not looking great.


But this is what I know so far:

to set a variable you type $variable1 = "James"

and then I could show that on a page as html by using the echo function: echo $variable1 and just the word James would come out on the page.

And I think I know how to make an array

$array1 = array("James","Bob",7)

And i could echo the second part of this array by typing echo $array1[1] (I know that php starts from the number 0).


And I also (think) I know how to make another type of array:

$array2 = array("Regular" => 300,"Medium" => 350,"Large" => 450)

And if i echoed $array2[Large] I would get 450 come up.

Now I beleive that I would also be able to learn some functions (if I learned them).



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
sousousou
Forum Commoner
Posts: 29
Joined: Fri Aug 28, 2009 1:10 pm

Re: New to php

Post by sousousou »

One of the first hits when I searched for "PHP form" was this one:

http://www.tizag.com/phpT/forms.php
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: New to php

Post by califdon »

The web is filled with tutorials. Many of us think that one of the best places to go for this is http://w3schools.com/.

You're correct about the PHP facts that you mentioned, but you must realize that that is maybe 0.0001% of what you need to know, just to write a simple login script, for example.
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: New to php

Post by tekkenlord »

thanks for the quick answer.

and oh - 0.00001% i've got a lot of work
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: New to php

Post by tekkenlord »

roughly, how long would it take me to learn php and mysql.

baring in mind that i have limited time (about an hour or two a day) cause i'm 15 and about to do my GCSEs
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: New to php

Post by tekkenlord »

and i would also like to know, where would be a good place to start?
sousousou
Forum Commoner
Posts: 29
Joined: Fri Aug 28, 2009 1:10 pm

Re: New to php

Post by sousousou »

I've learned from a book, PHP bible.
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: New to php

Post by tekkenlord »

is it easy to read, i'm not very good when it comes to books lol
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: New to php

Post by synical21 »

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.
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: New to php

Post by tekkenlord »

thanks so much AMAZING tutorial. and its 2.04am now, so i'd better go to bed lol. I'm going to do this in the morning though (or when i wake up)

btw when it is connecting to the database, what is the host:

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



i know that localhost is the one that is specific to my computer. but i have a website, so wondered how i would do it with that
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: New to php

Post by synical21 »

keep it as localhost, it should work, does for my site anyway :)
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: New to php

Post by tekkenlord »

oh thanks. did u learn php in 2 weeks??

and if you don't mind could i see ur website?
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: New to php

Post by synical21 »

Ill pm you hang on, and no i dont claim to know php yet :P these guys here "KNOW" php :)
Post Reply