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

The Add Term code

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

Saving the record and checking the table

ICT Secondary 2 Term 1, Unit 3, Topic 3: pp. 100–113
GoalSave the record with a prepared statement, show a message, and check the table.
New words
prepared statement · an SQL command written with ? placeholders; the values are sent separately and stay plain dataSQL injection · typed text that changes the meaning of an SQL command built by gluing text togethersuccess message · a sentence that tells the visitor the record was savedplaceholder · the ? in a prepared statement where a value will be put
Explanation

Last step: put the record into the table. The book glues the typed text straight into the SQL command. That is dangerous. Take the harmless term user’s guide: the glued command contains an apostrophe that ends the text too early, so the command breaks. And in the same way, text typed by a visitor could change what the command does. The remedy is a prepared statement. You write the command once with ? where the values go, and you send the values separately. The database treats them as plain data, so user’s guide is stored unchanged. The code below runs on an in-memory test database; for MySQL only the connection line changes. Then the page prints a message for the visitor: success, or “please fill in every field”. Finally, check that the record is really in the table by looking at the table in the database’s management page. Never assume: look.

StartAdd button pressed?yesnoall fields filled?yesnosave the picture and the recordshow: the term was savedshow: fill in every fieldshow the empty formEnd
Worked examples
Php
1<?php
2$term = "user's guide";
3echo "INSERT INTO Terms (Term) VALUES ('" . $term . "')";
4?>
Output
INSERT INTO Terms (Term) VALUES ('user's guide')
Gluing text into SQL. The harmless term user’s guide has an apostrophe. Look at the printed command: the text ends after user, and the rest, s guide, is left outside. The command is broken.
Php
1<?php
2$pdo = new PDO("sqlite::memory:");
3$pdo->exec("CREATE TABLE Terms (id INTEGER PRIMARY KEY, Term TEXT, Trans TEXT, Defe TEXT, Picture TEXT)");
4$stmt = $pdo->prepare("INSERT INTO Terms (Term, Trans, Defe, Picture) VALUES (?, ?, ?, ?)");
5$stmt->execute(["user's guide", "دليل المستخدم", "A booklet that explains a product.", "pic/guide.webp"]);
6echo $pdo->query("SELECT Term FROM Terms")->fetchColumn();
7?>
Output
user's guide
The prepared version. The command has four ? placeholders and the values go in a separate list. The apostrophe is stored unchanged: the query at the end prints the term exactly as typed.
Php
1<?php
2$pdo = new PDO("mysql:host=localhost;dbname=dbdictionary;charset=utf8mb4", "dict_user", "a-long-secret");
3?>
The connection line for MySQL, with an invented user and a placeholder password. The character set utf8mb4 lets Arabic terms be stored correctly. Everything else stays the same.
Php
1<?php
2$term = "router";
3$trans = "موجه";
4$defe = "Passes data between networks.";
5if ($term !== '' && $trans !== '' && $defe !== '') {
6    echo "The term was saved.";
7} else {
8    echo "Please fill in every field.";
9}
10?>
Output
The term was saved.
The precise code ends with a message. All three values are filled, so the test is true and the page prints the success message. With an empty value the else part would print the request to fill in every field.
idTermTransDefePicture
1routerموجهPasses data between networks.pic/router.webp
2cableكابلA wire that carries signals.pic/cable.webp
3browserمستعرضA program that shows web pages.pic/browser.webp
4keyboardلوحة مفاتيحKeys for typing letters and numbers.pic/keyboard.webp
After one INSERT the table has a new record (here id 4). The visitor can check it in the database’s management page. The changed row is highlighted.
Class activity

On paper write the final order of the Add Term page from the top to the bottom: includes, button test, copy of the values, empty test, picture checks, prepared INSERT, message. Mark the steps that are safety checks, and write the sentence the visitor sees after a good save.

Practice
1
What goes wrong when form text is glued into an SQL command and the text contains an apostrophe?
2
What is a prepared statement?
3
Name two things the page must tell the visitor.
4
How do you check that the record is really in the table?