linque
asked on
PHP - newbie needing help
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.
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.
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>
<?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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Edanisko. Before I end this question - does that code send the information to the database? how does it know where to go?
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"><?=$ad dress_info ;?></texta rea>
Once you submit,you get the info like this:
$address_info=$_POST[addre ss_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( ",",$addre ss_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
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"><?=$ad
Once you submit,you get the info like this:
$address_info=$_POST[addre
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(
$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
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,$zipc ode
To insert into sql,using example as below:
$query = "INSERT INTO address VALUES ('','".$street."','".$city ."','".$st ate."','". $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
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,$zipc
To insert into sql,using example as below:
$query = "INSERT INTO address VALUES ('','".$street."','".$city
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
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both very much!
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
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'
http://www.freewebmasterhelp.com/tutorials/phpmysql
good tutorial at this link
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
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
<?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>