Showing posts with label Topic 03. Show all posts
Showing posts with label Topic 03. Show all posts

Friday, May 29, 2009

Exercise 8: Answer

1. Start with a simple table in the database: In order to get this to happen I needed to create a database and then create a table 'employees'
DROP TABLE IF EXISTS `exercise8`.`employees`; CREATE TABLE `exercise8`.`employees` ( `first` varchar(45) NOT NULL, `last` varchar(45) NOT NULL, `address` varchar(45) NOT NULL, `position` varchar(45) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert a record
insert into exercise8.employees(first,last,address,position) values('First','Last','Address','Position');
run the query
SELECT * FROM exercise8.employees e;
and the result surprise, surprise is the first record from the employees table. 2. Create a Webpage
Here is a screenshot of the code and the webpage output

3. and 4. Add a record
Here is a screenshot of the webpage output and the code
5. Retrieve multiple records
Here is a screenshot of the webpage output and the code

Exercise 7: Answer

1. Short but simple. They say a picture is worth a thousand words, so here is 2000.

2.

Exercise 6: Answer

1 and 2 combined An image of the webpage output and the code that produced it:

Wednesday, May 27, 2009

Exercise 6: First attempt

Well found it: " ?> It definitely helps when you get the syntax correct. The answer to my question was sourced from http://au2.php.net/manual/en/reserved.variables.server.php In my mission to find out how to use php, I created the hello_world application. It didnt provide any surprises at all. And no problems either.

Exercise 6: First attempt

Well just going from the notes I thought I would just create a html file and open it in a browser. (did I tell you that what I know about php was what was written in the exercise?) So I dutifully copied the exercise code into notepad, saved it as exercise6.html, and opened it in IE8. I am sure that you will all be as surprised as I was to see that the screen was blank. Well so much for the short cut. Ok off to the web site http://php.net. I have now downloaded and installed WampServer 2.0h available from http://www.wampserver.com/en/download.php This has loaded Apache 2.2.11,PHP 5.2.9, SQLitemanager,MySQL 5.1.33 Phpmyadmin As to what I do with any of this. I am sitting here scratching my head with not the first clue as to what needs to happen now. I have WAMP started and online. Well finally I have gotten the page to display. It doesnt work but thats another issue. I am surprised that I have any hair left. None of that was obvious. (Although in hindsite it should have been). My MySQL issues are continuing however. I get the following message Access denied for user 'root'@'localhost' (using password:NO) trying to find the connection string settings in the php.ini file is like looking for a needle in a haystack. I think i would rather find the needle. When trying to display the file for exercise 6 I was getting the Undefined variable REMOTE_ADDR which makes sense as Ken said in the notes that this is the UNIX environment variable. So I should be able to change it with the php equivalent of _SERVER["REMOTE_ADDR"] WRONG! Maybe it is a good time to ask for help. What is wrong with this code ?????: "; ?> What should this look like?

Sunday, May 17, 2009

Exercise 8: PHP and MySQL database access

  1. Start with a simple table in the database:
    mysql> SELECT * FROM employees;
  2. Create a web page with the following PHP:
    <?php
    $db = mysql_connect(“farrer.csu.edu.au", “keustace", “password");
    mysql_select_db(“mydatabase",$db);
    $result = mysql_query("SELECT * FROM employees",$db);
    echo "First Name: ", mysql_result($result,0,"first"), "<BR>";
    echo "Last Name: ", mysql_result($result,0,"last"), "<BR>";
    echo "Address: ", mysql_result($result,0,"address"), "<BR>";
    echo "Position: ", mysql_result($result,0,"position"), "<BR>";
    ?>
    My password is included? We look at security later…☺
  3. This is how we can add a record and is part of a file to create called add_record.html
    <HTML>
    <BODY>
    <FORM METHOD="POST" ACTION="add_record.php">
    First name:<INPUT TYPE="Text" NAME="first"><br>
    Last name:<INPUT TYPE="Text" NAME="last"><br>
    Address:<INPUT TYPE="Text" NAME="address"><br>
    Position:<INPUT TYPE="Text" NAME="position"><br>
    <INPUT TYPE="Submit" NAME="submit" VALUE="Enter information">
    </FORM>
    </BODY>
    </HTML>
  4. The corresponding PHP file is add_record.php used with the POST method:
    <?php $db = mysql_connect(“farrer.csu.edu.au", “keustace", "password");
    mysql_select_db(“mydatabase",$db);
    $result = mysql_query("INERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')");
    if ($result == 1) { echo "Thank you! Your information has been entered.“;
    } else {
    echo "Sorry, there's a problem";
    }
    ?>
  5. The last code example shows how to get multiple records:
    <?php
    $db = mysql_connect(“farrer.csu.edu.au", “keustace", "password");
    mysql_select_db(“mydatabase",$db);
    $result = mysql_query("SELECT * FROM employees",$db);
    echo "<table border=1>\n";
    echo "<tr><td><b>Name</b></td><td><b>Position</b></tr>\n";
    while ($myrow = mysql_fetch_row($result)) {
    echo "<tr><td>", $myrow[2], ", ", $myrow[1], "</td><td>", $myrow[3], "</td></tr>";
    }
    echo

Exercise 7: User input for database access with PHP

  1. Create an HTML page with the form:
    <FORM METHOD="GET" ACTION="submit.php"> What's your name? <INPUT NAME="myname" SIZE=10>
    (Then press RETURN)
    </FORM>
  2. Then, create a PHP file named submit.php with the following code:
    <?php
    echo "Hello, ", $myname;
    ?>

Exercise 6: Some server practice with PHP

  1. In this exercise with can call up and examine some environment variables on the server. The code below has the UNIX environment variable for storing your remote Internet address. UNIX environment variable are recognised by the use of upper case letters. Try the same code by replacing with $REMOTE_ADDR $SERVER_NAME, or $PHP_SELF
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <?php
    echo "You are connected from: ",$REMOTE_ADDR;
    echo "<BR>";
    ?>
    </BODY>
    </HTML>
  2. Create a web application called "hello_world.php" which contains in the body:
    <?php
    $myvar = "Hello World!";
    echo $myvar;
    ?>