إعلانيمكن أن تكون علامتك التجارية هناعلامتك التجارية هنافي كل صفحة من Darslab · طلاب وأولياء أمور ومعلمونinfo@darslab.comاسأل عن الأسعارالأسعارinfo@darslab.com
☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

The Add Term code

الأسبوع 14 · الوحدة ٣ «إنشاء صفحات الموقع» (٧) الموضوع الثالث صفحة Add_Term.php: الكود المختصر وما ينقصه، الكود الأدق بالفحص والرسائل، حفظ السجل والتأكد من الجدول · 3 دروس
1

The short code and what it misses

ICT Secondary 2 Term 1, Unit 3, Topic 3: pp. 100–113
GoalRead the short Add Term code and say what it misses.
New words
$_POST · the array in which PHP keeps the values sent by a post form, found by control name$_FILES · the array that describes a file sent by the form: its name and its temporary placeisset · asks whether a variable or array entry exists and is not nullfile upload · moving the file the visitor chose from its temporary place into the site’s folder
Explanation

Now we put the parts together. Remember Week 13: the form sends its values to add_term.php. The short code begins with the two includes, header and connection. Then isset($_POST['add_btn']) asks “was the Add button pressed?”, because the page also runs when it is first opened with no data. If so, the code copies the values from $_POST using the control names. A picture arrives differently: $_FILES['picture'] holds its name and a temporary place on the server, and move_uploaded_file moves it from there to the folder pic. The path of the picture is then 'pic/' joined to the name. Finally the record goes to the database; that is Lesson 3. This code works only in a perfect case. It has three weaknesses: the folder pic may not exist, empty fields would be saved as an empty record, and the visitor gets no message at all. The precise code of the next lessons fixes all three.

What is checkedShort codePrecise code
the pic folder existsnoyes
no field is emptynoyes
the picture name is safenoyes
the record is saved safelynoyes
the visitor gets a messagenoyes
Worked examples
Php
1<?php
2include("header.php");
3include("connection.php");
4if (isset($_POST['add_btn'])) {
5    $term = $_POST['term'];
6    $trans = $_POST['trans'];
7    $defe = $_POST['defe'];
8    $name = $_FILES['picture']['name'];
9    move_uploaded_file($_FILES['picture']['tmp_name'], 'pic/' . $name);
10    // the INSERT comes in Lesson 3
11}
12?>
The short code, step by step. Fragments are shown without the final INSERT. Read each line and say which weakness it hides.
Php
1<?php
2if (!is_dir('pic')) {
3    mkdir('pic');
4}
5?>
Weakness 1 and its fix: if the folder does not exist, the upload fails. Check first and create it. (The book’s figure has a typo, mkdin; the real function is mkdir.)
Php
1<?php
2$term = "";
3$trans = "";
4if ($term !== '' && $trans !== '') {
5    echo "save";
6} else {
7    echo "message: fill in all fields";
8}
9?>
Output
message: fill in all fields
Weakness 2: with empty fields the short code would save an empty record. The precise code tests every value first. Here both fields are empty, so the test fails and the page prints a message instead of saving.
Class activity

On paper write the three weaknesses of the short code as three sentences, and next to each one the one line of PHP that fixes it (the folder test, the empty test, a message). Check that every line ends with a semicolon.

Practice
1
What does isset($_POST['add_btn']) ask?
2
Where does PHP find a chosen picture: $_POST or $_FILES?
3
What does move_uploaded_file do?
4
Name the three weaknesses of the short code.