Tag Archives: mysql

MySQL: Moving columns from one table to another

Here are the steps you can use to move a column from one table to another (within the same database) using MySQL commands.

 

1) Create a new column in the table you are moving your column to. You will want it to make sure the new column is created to hold the same type of data as the column you are moving. Here is a code example:

alter table destinationtable add column destinationcolumn varchar (250) ; 

2) Now, we will copy the contents of the existing column to the new column we just created:

INSERT INTO `destinationtable`( destinationcolumn )
SELECT existingcolumn
FROM `existingtable`

3) You can also add a condition to the statement if you are moving multiple columns and need to make sure that the data is placed correctly:

UPDATE `destinationtable`,`existingtable` SET destinationtable.destinationcolumn=existingtable.existingcolumn
WHERE destinationtable.productid=existingtable.productid

 

 

Combine the values of two fields in a form into one variable to be inserted into MySQL database table

Here is the simple code that I use to combine the values of two fields in a form into one variable and insert that variable into a database table.

One example of how I used this trick was when I built an SMS-subscriber widget for a website. I needed people to enter their phone numbers into one field, select their phone carrier from another field, and then combine those two fields with an “@” symbol in between to create an email address that was then inserted into the database. These SMS subscribers could then receive the same notice as email subscribers.

Here is the code you use:

<script>
$('#insert').bind('submit', function(){
                        var phone = $('[name=phone]').val();
                        var email = $('[name=email]').val();
                        $('[name=subscriber]').val(phone+'@'+email);
                      });
</script>

Note: #insert in the code above needs to be changed to the id of your form. 

Insert the jQuery code above directly after the beginning of your form, which would look something like this:

<form action="insert.php" method="POST" id="insert">

Next, create a hidden field which will contain the combined values of the two fields:

<input type="hidden" name="subscriber"/>

And finally, create the two fields whose values you want to combine:

Phone #: <input type="text" size=25 name="phone">
Carrier: <select>
  <option name="email" value="vtext.com">Verizon</option>
  <option name="email" value="txt.att.net">AT&T</option>
</select>

In the php file that you use to process the form, you will only need to post the data from the hidden field (which contains the values of the two fields). For instance, using the example above, I would only insert the ‘subscriber’ data into the database:

$v_subscriber=$_POST['subscriber'];
... 
$query="insert into table_name(email) values('$v_subscriber')";

This would insert the value of the subscriber field (which is “phone@email” according to the jQuery above. For example: 1234567890@att.txt.net) into the email column of the table you specified.

Please let me know if you have questions or run into any issues at all.

Developing a custom store locator map: Creating and populating the MySQL table

This is the second post in my series on how to create your store locator map using Google maps, PHP, and Javascript.

Now that we have our store locations in a usable format, we need to create the table on our website’s database and import the data for the locations.

Log into the phpMyAdmin account for your website. Create a new table – for this example, we will call the table Sheet1.

Add the columns to the table that you want to use in your store locator map, i.e.: store name, address, phone number, website address, longitude, and latitude. Make sure that you add columns for longitude and latitude – use float for the type and (10,6) for the length. These values will let the longitude and latitude fields store 6 digits after the decimal, plus up to 4 digits before the decimal.

Here is a screenshot of an example table (from http://code.google.com/apis/maps/articles/phpsqlsearch.html):

If you prefer to create the table by writing an SQL commands, use something like this:

CREATE TABLE `Sheet1` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR( 60 ) NOT NULL ,
  `address` VARCHAR( 80 ) NOT NULL ,
 `phone` VARCHAR( 80 ) NOT NULL ,
`website` VARCHAR( 80 ) NOT NULL ,
  `lat` FLOAT( 10, 6 ) NOT NULL ,
  `lng` FLOAT( 10, 6 ) NOT NULL
) ENGINE = MYISAM ;

 

Once you have the table and the columns you want created, it is time to import your locations. Import the csv or xls file that we created in our last post. Now all of your store locations should be stored in your site’s database.

The next step will be creating a PHP page which will be used to connect the store locator on our website to the database of locations that we just created.

Here are links to all of the posts in this series on how to create your own store locator map:

 

Create a spreadsheet of your store locations

Create and populate the MySQL table

Create a PHP file which will be used to connect to the database

Create a PHP file which will output the XML file results of a search

Create the HTML page which contains the store locator map

 

 And here are some hints and tips to help create and customize your store locator map:

Using your own custom markers for the locations in place of Google’s default markers

How to prevent the map from zooming in to close on a single location