.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # bash-tutorials
2 | Bash Tutorials to accompany YouTube series
3 | https://www.youtube.com/playlist?list=PLzZ0JczEkZfkvvycruN04zx6NmYYaavQI
4 |
--------------------------------------------------------------------------------
/ideas.txt:
--------------------------------------------------------------------------------
1 | 7-Reading / Writing to Files
2 | 8-Script Arguments
3 | 9-Read all files in a folder, inc certain types
4 | 10-Find a file
5 | 11-What Time is it
6 | 12-Check a program exists
7 | 13-Error Exit Codes, function return values
8 | 14-Read variables from a file
9 | 15-Menus
10 | 16-Am I Root
11 | 17-Elevate privileges
12 | 18-Is this script already running?
13 | 19-Package manager
14 |
--------------------------------------------------------------------------------
/phone.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
52 |
53 | Test Phonebook
54 |
55 |
56 |
57 |
58 | 0) {
70 | $ph_firstname = $_POST['ph_firstname'];
71 | }
72 | else {
73 | die ('Invalid first name');
74 | }
75 | if (preg_match('/^[\w\d\s\-\._+]+$/', $_POST['ph_surname']) > 0) {
76 | $ph_surname = $_POST['ph_surname'];
77 | }
78 | else {
79 | die ('Invalid surname');
80 | }
81 | if (preg_match('/^[\d\s\-+]+$/', $_POST['ph_number']) > 0) {
82 | $ph_number = $_POST['ph_number'];
83 | }
84 | else {
85 | die ('Invalid number');
86 | }
87 |
88 | $cmd = 'INSERT INTO phone(id, surname, firstname, number) VALUES (\'null\', \''.$ph_surname.'\', \''.$ph_firstname.'\', \''.$ph_number.'\')';
89 | if ($db->query($cmd) === true) {
90 | echo 'New record created successfully';
91 | }
92 | else {
93 | die('Error: '.$db->error);
94 | }
95 | }
96 | //-------------------------------------------------------------------
97 | function draw_search() {
98 | echo ''.PHP_EOL;
101 | }
102 | //-------------------------------------------------------------------
103 | function search_record($search) {
104 | global $db;
105 | //' OR '1=1'; INSERT INTO phone(id,surname,firstname,number) VALUES('null','konsole','test','222')'
106 | $query = "SELECT * FROM phone WHERE surname = '$search'";
107 |
108 | echo $query.'
';
109 |
110 | if(!$result = $db->query($query)){
111 | die('There was an error running the query'.$db->error);
112 | }
113 |
114 | print_r($result);
115 | if ($result->num_rows == 0) {
116 | echo 'Nothing found';
117 | }
118 | else {
119 | while($row = $result->fetch_assoc()) {
120 | echo ''.PHP_EOL;
121 | echo 'Surname:'.$row['surname'].'
'.PHP_EOL;
122 | echo 'Firstname:'.$row['firstname'].'
'.PHP_EOL;
123 | echo 'Number:'.$row['number'].'
'.PHP_EOL;
124 | echo '
'.PHP_EOL;
125 | }
126 | }
127 |
128 | $result->free();
129 | }
130 | //-------------------------------------------------------------------
131 | function show_form() {
132 | echo ''.PHP_EOL;
133 | echo ''.PHP_EOL;
139 | echo '
'.PHP_EOL;
140 | }
141 | //-------------------------------------------------------------------
142 | function show_all() {
143 | global $db;
144 |
145 | $query = 'SELECT * FROM `phone` ORDER BY `surname` DESC';
146 |
147 | if(!$result = $db->query($query)){
148 | die('There was an error running the query'.$db->error);
149 | }
150 |
151 | if ($result->num_rows == 0) {
152 | echo 'Nothing found';
153 | }
154 | else {
155 | while($row = $result->fetch_assoc()) {
156 | echo ''.PHP_EOL;
157 | echo 'Surname:'.$row['surname'].'
'.PHP_EOL;
158 | echo 'Firstname:'.$row['firstname'].'
'.PHP_EOL;
159 | echo 'Number:'.$row['number'].'
'.PHP_EOL;
160 | echo '
'.PHP_EOL;
161 | }
162 | }
163 |
164 | $result->free();
165 | }
166 | //-------------------------------------------------------------------
167 | $db = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
168 |
169 | draw_search();
170 |
171 | if ((isset($_POST['ph_surname'])) && (isset($_POST['ph_firstname'])) && (isset($_POST['ph_number']))) {
172 | add_record();
173 | }
174 | elseif (isset($_POST['ph_search'])) {
175 | $search = preg_replace('/[^\w\d\s\.\-_+]/', '', $_POST['ph_search']);
176 | search_record($search);
177 | }
178 | else {
179 | show_form();
180 | show_all();
181 | }
182 |
183 | $db->close();
184 | ?>
185 |
186 |
187 |
188 |
--------------------------------------------------------------------------------