Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Wednesday, September 7, 2011

HTML WebForm with PHP and MySQL - Part 4

Part 4 - PHP Script to insert data into database
Link
Recap: Part 1 | Part 2 | Part 3


Now that we have the database and all of the columns created, we now need to get that data into the database. We can do that with a PHP script.

First we need to build a PHP file that we can edit. Create a new file on your webserver and name it whatever you want (I called mine "pushtodb.php"). Now open that file with your editor of choice. I like notepad++ but you can use whatever is more comfortable for you.

Just like in HTML, we have to open the PHP script with <?PHP (and don't forget to close the tag now so you don't forget later - close it with ?>  ). This tells the browser that you are going to use PHP as oppose to just HTML. 


Lets create a variable to open up a connection to the SQL server. You do that with this command:

     $con = mysql_connect("SQL_server_name","username","password");

Now we have to tell the SQL server which database to use. This command will connect to the SQL server and open up the proper database:

     mysql_select_db("database_name", $con);


Notice the "$con" in that statement? That will take the first statement that we wrote and inject it into this one. Trust me, putting things in variables will make your life a LOT easier as you can reuse the variables throughout the script. 

Time to write that SQL statement that we will actually insert values into the database. Let's make this in a variable named "$sql" so we can use it over and over. At this point (if you read Part 1) you should have all your inputs from your webform in a text file so we can input them in the SQL statement. You have them in notepad++ right??? Right??? Here is the SQL statement; copy and paste this into notepad++ so you can edit it as you need to:



     $sql="INSERT INTO table_name (input_1, input_2, input_3)
     VALUES
     ('$_POST[input_1]','$_POST[input_2]','$_POST[input_3]')";

Ok we have all the connections out of the way, now time for use to actually perform the command(s). Here is the code that will take all the variables we defined and put them in a IF statement:
     if (!mysql_query($sql,$con))
     {
     die('Error: ' . mysql_error());
     }
One more piece of code that we will need now but will make more sense later on:
      $id = mysql_insert_id ( );
Whew. Come on Bob aren't we done yet? Nope, we have three more steps to finish. 
1. Assign the URL that we want people to go to after the script has been ran
2. Setup the email that will alert us when something has been submitted
3. close the SQL connection
Task 1:  Assign the URL

Put this in a variable, I used the variable "$url" but you can use whatever helps you remember what it is.

     $url = 'http://www.domain.com/thankyou.html';

The command that will actually take users to the next page is (look for that variable):

     echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';



Task 2: Setup the email

You are going to see variables from above in this one. You can see why variables are important.

     $emailadd = 'email@yourdomain.com';
     $subject = 'New contract was submitted';
     $body = "This is the body of the email:";
     $text = $body. " " . $id;

     mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

Pay attention to the bottom line. That is the actual command that will send the email out. As you can see, we are building the email with all the variables we make right above it. See that "$id" variable there. I put that in the email in a future lesson we will use that to lookup this form. We need a place holder that we can call this specific form from the database, and since ID is always unique, its the perfect one to use. 



Task 3: Closing the SQL connection (the easy one)

You are going to find this a real challenge. Here is the code (look for the variable that we assigned earlier):

    mysql_close($con)


At the bottom of the script you should see (if you followed directions from above) the ?> to close the PHP script. 

That's it. Post that to your site and give it a shot. Let me know if it works!!!! Good luck.

Thursday, August 25, 2011

Showing Advanced Features in Active Directory


I edit SPN's a lot with MS SQL server and I was sick of using ADSIedit so I found that the ADMINPAK for Windows 7 would allow me to edit the SPN's directly. Only problem is that you can't see the "Attributes" tab in Active Directory Users and Computers (ADUC) without turning on the advanced features. Here is how I turned them on:

1. Open Active Directory Users and Computers (Start -> Run -> type: dsa.msc -> Ok)

2. Click on "View" then check "Advanced Features"



Now you can see the "Attributes" tab as well as a lot of other tabs that make editing user profiles a lot easier for the lonely sys-admin.

Wednesday, July 27, 2011

HTML WebForm with PHP and MySQL - Part 1

A friend of mine asked me to create a form on his website so users could submit contracts to him without printing/faxing/attaching to email/etc..... So I said sure. I created a HTML webform and had it emailed to him every time someone clicked submit via a PHP script. Life was happy and he was happy until he started to receive them via plain text and it was unreadable to a non-IT person. So I needed to put it in a more "user friendly" format.

I started with looking for a PHP script that would email a friendlier version with no luck. I found a couple of people that tried but nothing that wasn't super hard to use. It showed very quickly that going down that road wasn't the best. So I decided to put it in a SQL database. His hosting provider gave him 3 databases to use on a shared MySQL server.

I am creating a series of blogs on how I accomplished this task. Stay tuned for more but for now, here is how I wrote the HTML web form.

Preview of the site: http://spinaroundsounddj.com/Contract_Form.html


Part 1

The first step is to create the HTML form. My tasks were to create a "Contracts" webform that looked like his other pages on his site. I had access to his site so the easiest way to start was to get a copy of another page and "Save-As" to a page called Contracts.html. Now that I have a shell of his page (top links, graphics, etc...) I can clean out the middle stuff and add the webform.

Important note to learn now and not later like I did: PHP and MySQL don't do well with CAPTIAL letters. Put everything in lowercase letters - you will be glad you did later.

Start the form with an HTML tag of

<form action="sendresults.php" method="post" name="contractform" id="contractform">
</form>


This will set the form up for you. Out of habit, I always close the HTML tag every time I open one. Just saves you from having to do it later. Let me explain a little about what this command does.

action="nameofphpscript.php" - This is what the submit button will do when it is clicked.
method="post" - You get two options here; POST or GET. very simply, POST puts stuff somewhere and GET gets stuff from somewhere.
name="contractform" - This is the name of the form.
id="contractform" - This is the id of the form

Now you can create all your tables and and inputs remembering to name them with lowercase letters. I set all of my inputs to [type="text"] just to make it easier. Functionality first and make it pretty later!!!

Helpful hint: If I were in your shoes, I would start a list of all the inputs that you make, it will pay off in the PHP script and inserting into SQL part of the blog series.

Now for the all important SUBMIT and RESET buttons. Here is the code:

<input type="submit" name="submit" id="submit" value="submit" />
<input type="reset" name="reset" id="reset" value="reset" />


So that will create (2) buttons on our form. One will submit the form to the ACTION="nameofphpscript" that we defined earlier. The other will clear all fields on the form so the user can start all over. The way they are defined by their function is the value="reset" tag in the code.

Now we should have a webform that looks the way that you want it to. Now we have to do something with it once they click submit. Stay tuned as that is the next part of this blog series.