Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.

Similar presentations


Presentation on theme: "Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured."— Presentation transcript:

1 Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured Query Language, and is used by many database, not just MySQL. SQL’s basic commands include INSERT – add a new record UPDATE – change an existing record SELECT – retrieve record(s) from the database DELETE – remove a record(s) from the database

2 Intro to DatabasesClass 4 DELETING A RECORD

3 Intro to DatabasesClass 4 Deleting a Record To delete a record, we have to identify what record to delete $query="DELETE FROM table WHERE x='y' ";

4 Intro to DatabasesClass 4 Deleting a Record To delete a user, we need to write a new script, called “delete_user.php” We also need to add a link to delete user from the “view_users.php” script 1- open view_users.php

5 Intro to DatabasesClass 4 in view_users.php List of all registered users '; // check that there are results $num_rows=mysql_affected_rows($link); // output content if($num_rows>0){ // get info while ($myrow = mysql_fetch_array($result)) { $user_id=$myrow["user_id"]; $user_name =$myrow["user_name"]; etc… print("$user_id | $user_name |… delete user "); } }else{ // tell user there's no info print("sorry, no records"); } … ?> Deleting a Record – Note we are passing a variable in the url string. This is the GET method

6 Intro to DatabasesClass 4 Deleting a Record 2- open delete_user.php

7 Intro to DatabasesClass 4 Deleting a Record in delete_user.php The user ID is missing "; } // Do I PASS ERROR CHECKING? if ($error_message=="" ) { // If everything's okay. ## DATABASE FUNCTIONALITY WILL GO HERE // 2- formulate the question // 3- ask the question // 4- output results }else{ // there's an error - print error message echo ' '.$error_message.' '; } … ?> – Error checking – make sure we have a user id to delete This time we're opening the database connection at the start of the page. Here it doesn't make much of a difference, but will make things easier as our scripts get more complicated

8 Intro to DatabasesClass 4 Deleting a Record in delete_user.php – Do deletion Note that this uses $_GET $id specifies what user to delete

9 Intro to DatabasesClass 4 Deleting a Record in delete_user.php A fancier ending – here we redirect back to the “view_users.php” page $_SERVER['HTTP_HOST'] - the server dirname($_SERVER['PHP_SELF']) – the directory viewusers.php – the page –

10 Intro to DatabasesClass 4 Deleting a Record 3- test both files

11 Intro to DatabasesClass 4 USING SELECT PART II

12 Intro to DatabasesClass 4 Selecting a specific record To allow users to log in, we need to determine if their user name and password exist in the database. To do this we use SELECT with a WHERE clause WHERE allows us to qualify what records we get AND allows us to include more than one requirement For example $query ="SELECT * FROM $tableName WHERE user_id=5"; $query ="SELECT * FROM $tableName WHERE user_name=‘Martin’ AND password=’12qwerty’"; –

13 Intro to DatabasesClass 4 Selecting a specific record When we ask a user to log in, we’re using the username and password sent through the form to get a specific user id. Our query will look like $query="SELECT user_id FROM $tableName WHERE user_name=‘$user_name’ AND password=‘$password’ "; – Column Names

14 Intro to DatabasesClass 4 Selecting a specific record 1- open login.php –

15 Intro to DatabasesClass 4 Selecting a specific record login.php '.$error_message.' '; // Display the form. print_login_form(); } } else { // Display the form. print_login_form(); } // End of the main SUBMIT conditional. –

16 Intro to DatabasesClass 4 Selecting a specific record login.php … ### USER DEFINED FUNCTIONS function print_login_form(){ print(" Please Login: User Name: Password: "); } ?> –

17 Intro to DatabasesClass 4 Selecting a specific record login.php // BASIC ERROR CHECKING // USER NAME if (strlen($_POST['user_name']) You forgot to enter your username! "; } // PASSWORD. if (strlen($_POST['password']) You forgot to enter your password! "; } –

18 Intro to DatabasesClass 4 Selecting a specific record login.php Welcome back '.$myrow[“first_name"].' '. $myrow["last_name"]; else{ // no records were found echo ' That username and password couldn\'t be found '; // Display the form. print_login_form(); } (mysql_num_rows($ result) – returns the number of records retrieved. In this case it should be 1 and only 1 –

19 Intro to DatabasesClass 4 Selecting a specific record Upload to the server and test for errors –

20 Intro to DatabasesClass 4 USING UPDATE

21 Intro to DatabasesClass 4 Using Update Update allows us to change existing records The basic query is UPDATE with a WHERE clause WHERE allows us to qualify what records we update SET tells the database what to change For example $query =“UPDATE $tableName SET password=‘$password’ WHERE user_id=5"; – Column Names

22 Intro to DatabasesClass 4 Using Update Open change_password.php –

23 Intro to DatabasesClass 4 Using Update changepassword.php That username and password couldn\'t be found '; // Display the form. print_changepass_form(); } }else{ // there's an error - print error message echo ' '.$error_message.' '; // Display the form. print_changepass_form(); } } else { // Display the form. print_changepass_form(); } // End of the main SUBMIT conditional. … ?> –

24 Intro to DatabasesClass 4 Using Update changepassword.php To change your password, please enter your current name and password and your new password: User Name: Password: NEW Password: "); } … ?> –

25 Intro to DatabasesClass 4 Using Update changepassword.php You forgot to enter your username! "; } // password if (strlen($_POST['password']) You forgot to enter your password! "; } // new password if (strlen($_POST['new_password']) You forgot to enter your NEW password! "; } … ?> –

26 Intro to DatabasesClass 4 Using Update changepassword.php <? … // PART 1 - CHECK FOR USER IN DATABASE & GET ID // PART 1a - formulate the question $user_name=$_POST['user_name']; // convert to make it easier $password=$_POST['password']; // convert to make it easier $query="SELECT user_id FROM $tableName WHERE user_name='$user_name' AND password='$password' "; // PART 1b- ask the question $result =mysql_query($query)OR die("error 3 - query failed".mysql_error()); –

27 Intro to DatabasesClass 4 Using Update changepassword.php Your password has been changed '; }else{ echo ' There has been an error. Please contact the webmaster '; } }else{ // there's an error – user not found echo ' That username and password couldn\'t be found '; // Display the form. print_changepass_form(); } … ?> –

28 Intro to DatabasesClass 4 INSERTING THE DATETIME WHEN A USER REGISTERS

29 Intro to DatabasesClass 4 Inserting the DateTime when a user registers register.php // 2- formulate the question $query="INSERT INTO $tableName (user_name, password, first_name, last_name, email, registration_date) values ('$user_name', '$password', '$first_name', '$last_name', '$email', now())"; View_users.php // output content if($num_rows>0){ // get info while ($myrow = mysql_fetch_array($result)) { $user_id=$myrow["user_id"]; $user_name =$myrow["user_name"]; $password =$myrow["password"]; $first_name =$myrow["first_name"]; $last_name =$myrow["last_name"]; $email =$myrow["email"]; $registration_date =$myrow["registration_date"]; print("$user_id | $user_name | $password | $first_name | $last_name | $email | $registration_date | "); ] –


Download ppt "Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured."

Similar presentations


Ads by Google