Forum Moderators: coopster

Message Too Old, No Replies

Trip Report PHP

         

outdoorxtreme1

5:02 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



I am a newbie to PHP. How would I go about creating a trip report submission.

Any help would be greatly appreciated. I want to create trip reports for my canoe club webpage.

Thanks in advance.

Dustin

[edited by: jatar_k at 5:28 pm (utc) on Oct. 18, 2005]
[edit reason] no urls thanks [/edit]

jatar_k

5:33 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld outdoorxtreme1,

you could start by using their form as a point of reference. You would then need a database to allow you to store the information and then later display it.

What part of the process are you having trouble with?

outdoorxtreme1

5:35 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



I am not sure how to display the information.

outdoorxtreme1

5:37 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Also, I am not sure on what I have to put on a page to get the info to the server from the form.

jatar_k

5:42 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well there are 2 completely different issues here

first we need to accept and insert the information
second we need to figure out how to display it

let's concentrate on the first one first

do you want to use a database?
if so is it mysql?

you would then need to create a database to store the information

then you would need to create a form to accept user input so you can insert that input into your database

There are some useful threads regarding using php and mysql in the PHP Forum Library [webmasterworld.com] that may help some

outdoorxtreme1

5:45 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Yes, I am going to use mysql and I have the database created.

I need help with setting up the form to accept the user input.

jatar_k

5:49 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



in essence the form will mimic the database though the database will usually have a bit more information.

as far as setting up the form that is just straight html then you set the action in your form tag to target your processing script which will handle input validation and inserting the information into the database.

do you have the html form created yet?

outdoorxtreme1

5:56 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Yes, I have the html form created. I am not sure if I have all thr right info I need on it though

outdoorxtreme1

6:01 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



I noticed that their form had a .php ending in the address bar

jatar_k

6:12 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



lots of people do that, it could mean a bunch of things

1. they use .php for every file whether there is any php in it or not
2. the form action posts to itself (something I don't recommend and I think is a bad idea)
3. they do something with php on the page which may, or may not, be related to the form

outdoorxtreme1

6:19 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



am I able to post some code on here for you to check out? I can't figure out how get the code from the form to mysql now. Do i need somehint at the begining of the code that links to my server?

jatar_k

6:25 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sure post a bit of your code

take a look at this thread
Basics of extracting data from MySQL using PHP [webmasterworld.com] msg 5

I realize you are inserting and not extracting at the moment but steps 1, 2 and 3 are the same anytime you are interacting with mysql.

connect, select db, query

what happens from there is the only difference

in this case as long as the query doesn't fail you are done.

outdoorxtreme1

6:28 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



<form method="post" action="/tct/submit.php">

<input id="Trip" type="submit" name="screen" value="Trip" />
<input id="Participants" type="submit" name="screen" value="Participants" />
<input id="Body" type="submit" style="font-size:smaller;width:7em;" name="screen" value="Body" onclick="self.clicked=this.value" />
<input id="Proofread" type="submit" style="font-size:smaller;width:7em;" name="screen" value="Proofread" onclick="self.clicked=this.value" />
<input id="Submit" type="submit" style="font-size:smaller;width:7em;" name="screen" value="Submit" onclick="self.clicked=this.value" />
<input id="Reset" type="submit" style="font-size:smaller;width:7em;" name="screen" value="Reset" />
<input type="hidden" name="responsepage" value="index.php" />

<input type="text" size="29" maxlength="50" name="triplocation" value="" />

Condition:
<select name="condition" size="1">

Difficulty:
<select name="pickClass" size="1">

Organizer:
<input type="text" size="29" maxlength="50" name="leadr" value="" />

Painted gauge:
<input type="text" size="5" name="rGaugeFT" value="" /> ft.

Start:<br />
<select name="pickMonth" size="1" onchange="setDOW(true)">
<select name="pickDay" size="1" onchange="setDOW(true)">
<select name="pickYear" size="1" onchange="setDOW(true)">
<select name="pickDays" size="1" onchange="setDOW(true)">

USGS gauge:
<input type="text" size="5" name="iGaugeFT" value="" /> ft
<input type="text" size="5" name="iGaugeCFS" value="" /> cfs

Finish:
<select name="pickMonth1" size="1" onchange="setDOW(false)">
<select name="pickDay1" size="1" onchange="setDOW(false)">
<select name="pickYear1" size="1" onchange="setDOW(false)">
<select name="pickDays1" size="1" onchange="setDOW(false)">

USGS Gauge Location:
<input type="text" name="iGaugeID" value="" size="29" maxlength="50" />

<input type="text" size="29" maxlength="50" name="author" value="" /> </td>

<input type="text" size="29" maxlength="50" name="email" value="" /> </td>

<input type="hidden" name="k1" value="" />
<input type="hidden" name="k2" value="" />
<input type="hidden" name="oc1" value="" />
<input type="hidden" name="oc2" value="" />
<input type="hidden" name="c1" value="" />
<input type="hidden" name="c2" value="" />
<input type="hidden" name="air1" value="" />
<input type="hidden" name="raft" value="" />
<input type="hidden" name="rpt" value="" />

</form>

Do I need a config.php file? I am really new to this but I am determined to make it work somehow.

[edited by: jatar_k at 7:23 pm (utc) on Oct. 18, 2005]
[edit reason] trimmed extra code [/edit]

jatar_k

6:30 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



actually that is just your form

the file doing the actual processing of the form will be

/tct/submit.php

outdoorxtreme1

6:35 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



ok. I think that is where I am having the trouble. I am not sure what all I need for the actual processor file.

jatar_k

6:37 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



start with the post I linked to above and look at the first three step

that should get you started

outdoorxtreme1

6:48 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Ok I have the steps 1 and 2 done. I'm not really sure on step 3

outdoorxtreme1

6:49 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



are there names in my form that have to match in mysql?

jatar_k

7:01 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> are there names in my form that have to match in mysql?

not at all, your script ill take the user input values and use those values to build a mysql insert query. That query will have to use the proper column names. The script will know both.

>> step 3

build your INSERT query [dev.mysql.com]
don't worry too much as I imagine that page is hard to understand but you should familiarize yourself with the mysql manual as well as with the PHP manual [php.net]

insert queries need to follow this form

INSERT INTO tablename (column1,column2,column3) VALUES ('value1','value2','value3')

these lists of columns can be as long as you want, they are not limited to only three. The location of the column in the first part of the query must match the location of the corresponding value in the VALUES portion of the query. It is a very common mistake to have the wrong number of values/columns or even to put them in the wrong order.

We then need a list of the columns and the names of the form elements that correspond to the columns

could you post that? not a ton of code, just the column names and what the form element name is that corresponds to it

dbcolumnname = formelementname

;)

outdoorxtreme1

7:05 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



do I get that info from the html part?

outdoorxtreme1

7:24 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



here is what I have so far. I took out the username, password and db name.

<?
$host = "localhost";
$user = " ";
$pass = " ";
$dbname = " ";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = "select * from tct where id=" . $_POST['manufacturer'] . " and manufacturer='" . $_POST['manufacturer'] . "'";
echo $sql;

?>

jatar_k

7:27 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



drop this

$sql = "select * from tct where id=" . $_POST['manufacturer'] . " and manufacturer='" . $_POST['manufacturer'] . "'";
echo $sql;

just don't worry about that bit right now

and take a look at your code post above, I ripped out all the unimportant stuff and left the elements only

so that will make it easier to map formfields to dbcols

outdoorxtreme1

11:45 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



What is in the name=" " ,is that the form elements?

jatar_k

12:36 am on Oct 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



exactly, like this one

name="triplocation"

that means when you submit this form the value of the above form element will be available to php as

$_POST['triplocation']

outdoorxtreme1

3:30 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Ok. I am able to post data to mysql and I see it in there now. How do I go about retrieving it?

outdoorxtreme1

3:31 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



WHat I want to do is have a page with the Trip location and date and to be able to click on it and view the full report.

jatar_k

3:36 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you now to construst your SELECT statement and would need some criteria to base your query on

what criteria are available on the page where you want to show the 'Trip location and date'?

are these done by user or are you just looking for a full list?

outdoorxtreme1

3:38 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



full list

jatar_k

3:51 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



full list is as easy as

SELECT * FROM tablename

steps involved are

1. connect to db
2. select db
3. construct query

$sql = 'SELECT * FROM tablename';

4. send query to mysql

$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

5. extract rows

while ($row = mysql_fetch_array($result)) {
// formatting goes in here in the format of $row['colname']
// to access individual columns
}

inside your while loop is where you format your ouput, for a test to take a look at what the output is like you could put a print_r inside your while loop

$counter = 1;
while ($row = mysql_fetch_array($result)) {
echo "<p>$counter: <pre>";
print_r($row);
echo '</pre>';
$counter++;
}

outdoorxtreme1

4:13 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Is this right?

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = 'SELECT * FROM tripreport';

$counter = 1;
while ($row = mysql_fetch_array($result)) {
echo "<p>$counter: <pre>";
print_r($row);
echo '</pre>';
$counter++;
}

This 158 message thread spans 6 pages: 158