?
Solved

PHP - newbie needing help

Posted on 2007-07-25
11
Medium Priority
?
192 Views
Last Modified: 2013-12-12
I need help learning PHP coding.  I have a lesson to do.  I have a simple html form designed to take street, city, state and zipcode.  Eventually, this information is to go into a database with the same fields plus and autoincrement ID.    I'll have to use a $_POST arrayand somehow get this information into a table in mySQL.

I'm not sure where to begin.  Would I create an array before the form?  I have one of O'Reilly's books here.  His example gathers information from a form with one text area, but I have several text areas (they must be text areas for this exercise).  There is an example for multiple answers from a check box in the book, but not for multiple values from text area boxes.

So I'm not certain where in my file to make this array, and I would appreciate a hint about what I need to do to send this to the database.  
0
Comment
Question by:linque
  • 4
  • 4
  • 3
11 Comments
 
LVL 2

Expert Comment

by:Edanisko
ID: 19566852
The $_POST  array is built in.  It is a premade part of the php language.  All u have to do is use it.  You project file will end up looking something like this.


<?php
$street = (isset($_POST['street'] ? $street : '');
$city= (isset($_POST[city] ? $city: '');
$state= (isset($_POST[state] ? $state: '');
$zip= (isset($_POST[zip] ? $zip: '');
?>
<form name=blah method=post>
<input type=text name=street value='<?php echo $street;?>'>
<input type=text name=city value='<?php echo $city;?>'>
<input type=text name=state value='<?php echo $state;?>'>
<input type=text name=zip value='<?php echo $zip;?>'>
<input type=submit>
</form>

0
 
LVL 2

Expert Comment

by:Edanisko
ID: 19566873
correction


<?php
$street = (isset($_POST['street']) ? $street : '');
$city= (isset($_POST[city]) ? $city : '');
$state= (isset($_POST[state]) ? $state : '');
$zip= (isset($_POST[zip]) ? $zip :'');
?>
<form name=blah method=post>
<input type=text name=street value='<?php echo $street;?>'>
<input type=text name=city value='<?php echo $city;?>'>
<input type=text name=state value='<?php echo $state;?>'>
<input type=text name=zip value='<?php echo $zip;?>'>
<input type=submit>
</form>
0
 
LVL 2

Accepted Solution

by:
Edanisko earned 1000 total points
ID: 19566883
ok sry last correction please delete earlier correction...

<?php
$street = (isset($_POST['street']) ? $street : '');
$city= (isset($_POST['city']) ? $city : '');
$state= (isset($_POST['state']) ? $state : '');
$zip= (isset($_POST['zip']) ? $zip :'');
?>
<form name=blah method=post>
<input type=text name=street value='<?php echo $street;?>'>
<input type=text name=city value='<?php echo $city;?>'>
<input type=text name=state value='<?php echo $state;?>'>
<input type=text name=zip value='<?php echo $zip;?>'>
<input type=submit>
</form>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:linque
ID: 19567116
Thank you Edanisko.  Before I end this question - does that code send the information to the database?  how does it know where to go?
0
 
LVL 2

Expert Comment

by:eechincs
ID: 19567176
Hi linque,
For your form,I assume that your street, city, state and zipcode information are all write inside the single text area field and each separated by comma for example: Street A,City B, State C, Zipcode D.And the text area you define in your html form is like this:
<textarea cols="40" rows="5" name="address_info"><?=$address_info;?></textarea>

Once you submit,you get the info like this:
$address_info=$_POST[address_info];

So now, the variable $address_info containing all the address information such as street,city,state and zipcode,the next step is to separate them into different field and store into the database.
$address_info_arr=explode(",",$address_info);
$street=$address_info[0];
$city=$address_info[1];
$state=$address_info[2];
$zipcode=$address_info[3];

(Remember,for this example,make sure each section of the information separated by comma is correct,meaning that first block of information before comma is street information,second block of information before comma is city information and so on.)

Then the final stage you just need to insert the information into the database by using the sql statement.If this is not what you want,please tell us so that we can help you again.

Regards,
eechincs

0
 
LVL 2

Expert Comment

by:eechincs
ID: 19567232
Hi linque,
The code will not send to the database, you need to use another query to insert into databse.For the following example,you will have four variables which contain the relevant data

$street,$city,$state,$zipcode

To insert into sql,using example as below:
$query = "INSERT INTO address VALUES ('','".$street."','".$city."','".$state."','".$zipcode."')";
mysql_query($query);

Since your primary key which is called ID is auto increment, so the sql statement need not indicate the data to be inserted while execute the query,just leave it blank.

Regards,
eechincs
0
 

Author Comment

by:linque
ID: 19567256
Hi eechincs,

Actually, the information says to make separate text area boxes.  I am sorry for the lack of clarity.  But yes, ok..  the SQL statement!  I can figure that part out I think.   So, therefore, should I do the form as Edanisko had suggested.  Thanks for helping me to understand all of this.
0
 
LVL 2

Assisted Solution

by:eechincs
eechincs earned 1000 total points
ID: 19567357
Hi linque,
I now start to understand your question:)
So,since all the different information is stored by separated text area boxes,yes,you can follow what Edanisko tell you, but need to change the field type from text boxt to text area, like below:

<?php
if($_POST[faction]=="insert")
{
$street = (isset($_POST['street']) ? $street : '');
$city= (isset($_POST['city']) ? $city : '');
$state= (isset($_POST['state']) ? $state : '');
$zip= (isset($_POST['zip']) ? $zip :'');

$query = "INSERT INTO address VALUES ('','".$street."','".$city."','".$state."','".$zipcode."')";
mysql_query($query);
echo "Your record insert successfully!";
}
?>
<form name=form method=post>
<input type="faction" value="insert">
<textarea cols="40" rows="5" name="street"><?=$street;?></textarea>
<textarea cols="40" rows="5" name="city"><?=$city;?></textarea>
<textarea cols="40" rows="5" name="state"><?=$state;?></textarea>
<textarea cols="40" rows="5" name="zipcode"><?=$zipcode;?></textarea>
<input type=submit name="submit" value="submit">
</form>

And also,you can add some validation to the form value before submit.Since it is not related to your question,then i will stop here.Hope this help you.

Regards,
eechincs
0
 

Author Comment

by:linque
ID: 19567700
Thank you both very much!
0
 
LVL 2

Expert Comment

by:Edanisko
ID: 19567732
eechincs,

that code doesnt send the information in a database.  to insert the code into a database requires the following.

1. a working database server
2. create a table in the database using a gui or the SQL command CREATE TABLE
3. google mysql php for a list of commands that will perform the queries.

you would create the query like this in php

$sql = "insert into yourtable (street, city, state, zip) values ('$street','$city','state','$zip')";

http://www.freewebmasterhelp.com/tutorials/phpmysql
good tutorial at this link
0
 
LVL 2

Expert Comment

by:eechincs
ID: 19576134
Edanisko:
Ya, you are right....i misunderstand the question,I thought he had set up the database server and had the table inside already and what he lack was just the sql query.Thanks you

Regards,
eechincs
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question