Help me get started with this simple form (php/mysql)
Alright, this is my first big step into PHP. I think this is a fairly simple application so I should be able to handle it, right?
What I have is a fairly simple form that I need to send to a database. Here is the form:
http://www.housing.uwaterloo.ca/profiles/individual.php
These are the steps I have to develop this:
1. create database
2. get form to send info to database
3. add file upload functionality (this might be trickier!)
4. make page that reads database (for now - will build on this later).
That's pretty simple, right? My first question is - does the PHP need to go into this page or does it need to be in a separate script that is called when the form is submitted? I'm guessing it's the latter. Or am I too clueless to do this?
Does anyone have any recommendations for beginner PHP sites? I've been googling around but haven't found anything suitable yet.
Megan posted this at 15:39 — 12th January 2007.
She has: 11,421 posts
Joined: Jun 1999
I'm now creating the mySQL database. Do I need to worry a lot about what field types I use? I know I need to estimate the right one, but some of them are confusing so I'm not sure which one to use (i.e. blob vs. longblob or something like that). Also, would it be bad to have all the information in one table? That seems to be the easiest solution to me.
Megan
Connect with us on Facebook!
Megan posted this at 17:07 — 12th January 2007.
She has: 11,421 posts
Joined: Jun 1999
More questions - should the code that puts the form entries into the database be on the form page or should it be on a separate page which is called when the form is submitted?
In the database, is it okay if the collation says "latin1_swedish_ci"? I know it put that in there as the default - should I change it to something else? If so, what?
Megan
Connect with us on Facebook!
Megan posted this at 17:48 — 12th January 2007.
She has: 11,421 posts
Joined: Jun 1999
I tried to set it up with help from this tutotiral. This is the code I have:
<?php
// include database info
include 'library/db_connect.php';
if(isset($_POST['submit'])) // if the form is submitted
{
// set variables for for the form
$name = $_POST['name'];
$hometown = $_POST['hometown'];
$program = $_POST['program'];
$year = $_POST['year'];
$residence = $_POST['residence'];
$email = $_POST['email'];
$query = "INSERT INTO profiles-individual (name, hometown, program, year, residence, email) VALUES ('$name', '$hometown', '$program', '$year', '$residence', '$email'";
mysql_query($query) or die('Error, insert query failed');
echo "Form fields added";
}
else { // if the form wasn't submitted, show the form
?>
<form method='post'>
<fieldset>
<legend>Personal Information</legend>
<ol id="personalinfo">
<li><label for='name'><span class="required">*</span>Name:</label>
<input type='text' size='30' name='name' id='name' value="" maxlength='255' />
<p class='help'>Note: just your first name is fine.</p>
</li>
<li>
<label for="hometown">Hometown:</label>
<input type='text' size='30' name='hometown' id='hometown' value="" maxlength='255' />
</li>
<li>
<label for='program'>Program:</label>
<input type='text' size='30' name='program' id='program' value="" maxlength='255' />
</li>
<li>
<label for='year'><span class="required">*</span>Academic year: </label>
<select name='year' id='year'>
<option value='' selected>Please choose..</option>
<option value='1'>First year</option>
<option value='2'>Second year</option>
<option value='3'>Third year</option>
<option value='4'>Fourth year or above</option>
<option value='5'>Grad student</option>
</select>
</li>
<li><label for='residence'><span class="required">*</span>Residence: </label>
<select name='residence' id='residence'>
<option value='' selected>Please choose..</option>
<option value='CLVN'>Columbia Lake Village North</option>
<option value='CLVS'>Columbia Lake Village South</option>
<option value='MKV'>Mackenzie King Village</option>
<option value='Minota'>Minota Hagey</option>
<option value='REV'>Ron Edyt Village</option>
<option value='UWP'>UW Place</option>
<option value='V1'>Village 1</option>
</select>
</li>
<li><label for='email'><span class="required">*</span>Email Address:</label>
<input type='text' size='30' name='email' id='email' value="" maxlength='255' />
<p class='help'>If we decide to publish your profile we will be contacting you for a photo. We <strong>will not</strong> publish your email address with your profile; it is only for our office to contact you if necessary.</p>
</li>
</ol>
<input class='submit' type='submit' value='submit' />
</fieldset>
</form>
<?php
}
?>
But it doesn't do anything? It's at this link:
http://www.housing.uwaterloo.ca/profiles/firsttest.php
Any help at all??? Please???
Megan
Connect with us on Facebook!
Megan posted this at 19:46 — 12th January 2007.
She has: 11,421 posts
Joined: Jun 1999
Well, all that, and it turns out I forgot to create a field for one of the entries in the database! Now I have it writing to the database
teammatt3 posted this at 19:46 — 12th January 2007.
He has: 2,102 posts
Joined: Sep 2003
I can't offer you any advice megan because I barely know PHP, but if you're really serious about learning PHP, you will probably want to do it yourself. Otherwise you could use PHP Form Wizard. I use it for all my forms and it is worth its weight in gold (appox $30). I could have your form done in a few minutes. Actually it *might* help you learn PHP because you could analyze what PHP code it gives you after you finish the wizard.
Megan posted this at 19:54 — 12th January 2007.
She has: 11,421 posts
Joined: Jun 1999
Question: If you're writing form data to a database, is it best to put all the post data into variables before putting it into the query?
Another one: I need to split the form so that certain questions only show up based on answers to previous questions. What would be the best way to do that?
Megan
Connect with us on Facebook!
andy206uk posted this at 21:18 — 12th January 2007.
He has: 1,758 posts
Joined: Jul 2002
Bloody hell... how many questions?
The page with the form doesn't need to be PHP however, I prefer to post my data back to the page with the form as I can then highlight fields that have failed validation and put the original input back into the boxes to save users retyping everything.
You should always use the right fieldtype for the job. For short text inputs use the varchar type but limit the size to match the expected input.
Leave the collation as it is... that's the mysql default and it mostly works fine.
It's not necessary to write the post vars to proper variables, but make sure you always use the mysql_escape_string function on any variables you insert to prevent mysql injection attacks.
I always find that if I have problems, the easiest solution is to echo out $query and copy it into phpmyadmin so I can see better what the error is.
Andy
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.