Database
Community
Admin
|
Note the use of $PHP_SELF again. Like I said in Lesson 1, you can use PHP anywhere inside your HTML code. You\ll also notice that each form element matches the field name in the database. This is not compulsory; it\s just a good idea so you can get your head around your code later on.
Also notice that I\ve given the Submit button a name attribute. I\ve done this so I can test for the existence of a $submit variable. That way, when the page is called again, I\ll know whether someone used this form.
I should mention that you don\t have to have a page that loops back on itself. You can span two, three, or more pages, if you like. This way everything stays compact.
OK, let\s add some code that will check for the form input. Just to prove that the form input does make it through, I\ll dump all the variables to the screen with $HTTP_POST_VARS. This is a useful debugging feature. If you ever need to see all the variables on a page, use $GLOBALS.
\n\; } } else{ // display form ?>
Now that this is looking good, let\s take the form information and post it to the database.
You\ve now inserted data into the database. It\s still far from perfect. What if someone leaves a field blank or enters text when we want a numeric entry? What if there\s an error somewhere?
Don\t worry. We\ll get to that.
Page 5 — Make the Forms Smarter
-------------------------------------------------------------------------------- Throughout this tutorial, I\ve been loading the SQL statement into a variable ($sql) before firing the query at the database with mysql_query(). This is useful for debugging. If something goes wrong, you can always echo the SQL to the screen to examine it for mistakes.
We already know how to get data into the database. Now let\s try modifying records that are already in the database. Editing data combines two elements we\ve already seen: displaying data on the screen and sending data back to the database via form input. However, editing is slightly different in that we have to show the appropriate data in the form.
First, let\s recycle the code from Lesson 1 to display the employee names on our page. But this time through, we\re going to populate our form with employee information. It should look a little like this:
%s %s \n\, $PHP_SELF, $myrow[\id\], $myrow[\first\], $myrow[\last\]); } } ?>
We just echoed the field information into the value attribute of the each element, which was fairly easy. Let\s build on this a little more. We will add the ability to send the edited code back to the database. Again, we\re going to use the Submit button to test whether we need to process the form input. Also note the slightly different SQL statement we use.
%s %s \n\, $PHP_SELF, $myrow[\id\], $myrow[\first\], $myrow[\last\]); } } ?>
And that\s that. We\ve managed to combine most of the features we\ve seen into one script. You can also see how we\ve used an if() statement inside another if() statement to check for multiple conditions.
It\s time to put it all together and make one killer script.
Page 6 — All Together Now
-------------------------------------------------------------------------------- We\ll finish up this lesson by putting everything into a single page that can add, edit, and remove entries from the database. It\s an extension of what we\ve covered so far and makes for a good review. Let\s take a look.
\; } elseif ($delete) { // delete a record $sql = \DELETE FROM employees WHERE id=$id\; $result = mysql_query($sql); echo \$sql Record deleted!
This looks complex, but it really isn\t. The script is broken up into three parts. The first if() statement checks to see whether the Submit button has been pressed, and if it has, it checks to see whether the variable $id exists. If doesn\t, then we\re adding a record. Otherwise, we\re editing a record.
Next we check to see whether the variable $delete exists. If it does, we delete a record. Note that with the first if() statement we checked for a variable that came through as a POST, and in this one, the variable would be part of a GET.
Finally, we take the default action that displays the list of employees and the form. Again we check for the existence of the $id variable. If it exists, we query the database to display the relevant record. Otherwise, we display a blank form.
We\ve now put all we\ve learned into one script. We used while() loops and if() statements, and we ran the gamut of the basic SQL statements - SELECT, INSERT, UPDATE, and DELETE. Lastly, we\ve looked at how we can pass information from one page to another using URLs and form input.
In Lesson 3 we\ll look at how to make the page more intelligent. |
No. |
제목 |
작성자 |
작성일 |
조회 |
---|---|---|---|---|
16601 | Transactions in MySQL (2) | 정재익 | 2002-07-23 | 9022 |
16600 | Transactions in MySQL (1) | 정재익 | 2002-07-23 | 12221 |
16599 | PHP/MySQL Tutorial (3) | 정재익 | 2002-07-23 | 10378 |
16596 | PHP/MySQL Tutorial (2) | 정재익 | 2002-07-23 | 10540 |
16595 | PHP/MySQL Tutorial (1) | 정재익 | 2002-07-23 | 32487 |
16582 | JSP/Servlet 에서 MySQL 접속 예제코드 | 정재익 | 2002-07-22 | 8315 |
16581 | NT4.0+JDK+JSDK+apache+tomcat+mysql+jdbc | 정재익 | 2002-07-22 | 8505 |