- PHP 7 Programming Blueprints
- Jose Palala Martin Helmich
- 486字
- 2021-07-08 11:14:10
Create a profile input form
Now let's create the HTML form for users to enter their profile data.
Our profiles app would be no use if we didn't have a simple way for a user to enter their user profile details.
We'll create the profile input form like this:
//create_profile.php <html> <body> <form action="post_profile.php" method="POST"> <label>Name</label><input name="name"> <label>Age</label><input name="age"> <label>Country</label><input name="country"> </form> </body> </html>
In this profile post, we'll need to create a PHP script to take care of anything the user posts. It will create an SQL statement from the input values and output whether or not they were inserted.
We can use the null coalesce operator again to verify that the user has inputted all values and left nothing undefined or null:
$name = $_POST['name'] ?? ""; $age = $_POST['country'] ?? ""; $country = $_POST['country'] ?? "";
This prevents us from accumulating errors while inserting data into our database.
First, let's create a variable to hold each of the inputs in one array:
$input_values = [ 'name' => $name, 'age' => $age, 'country' => $country ];
The preceding code is a new PHP 5.4+ way to write arrays. In PHP 5.4+, it is no longer necessary to put an actual array()
; the author personally likes the new syntax better.
We should create a new method in our UserProfile
class to accept these values:
Class UserProfile { public function insert_profile($values) { $link = mysqli_connect('127.0.0.1', 'username','password', 'databasename'); $q = " INSERT INTO " . $this->table . " VALUES ( '".$values['name']."', '".$values['age'] . "' ,'".$values['country']. "')"; return mysqli_query($q); } }
Instead of creating a parameter in our function to hold each argument as we did with our profile template render function, we can simply use an array to hold our values.
This way, if a new field needs to be inserted into our database, we can just add another field to the SQL insert
statement.
While we are at it, let's create the edit profile section.
For now, we'll assume that whoever is using this edit profile is the administrator of the site.
We'll need to create a page where, provided the $_GET['id']
has been set, that the user that we will be fetching from the database and displaying on the form. Here is how that code will look like:
<?php require('class/userprofile.php');//contains the class UserProfile into $id = $_GET['id'] ?? 'No ID'; //if id was a string, i.e. "No ID", this would go into the if block if(is_numeric($id)) { $profile = new UserProfile(); //get data from our database $results = $user->fetch_id($id); if($results && $results->num_rows > 0 ) { while($obj = $results->fetch_object()) { $name = $obj->name; $age = $obj->age; $country = $obj->country; } //display form with a hidden field containing the value of the ID ?> <form action="post_update_profile.php" method="post"> <label>Name</label><input name="name" value="<?=$name?>"> <label>Age</label><input name="age" value="<?=$age?>"> <label>Country</label><input name="country" value="<?=country?>"> </form> <?php } else { exit('No such user'); } } else { echo $id; //this should be No ID'; exit; }
Notice that we're using what is known as the shortcut echo
statement in the form. It makes our code simpler and easier to read. Since we're using PHP 7, this feature should come out of the box.
Once someone submits the form, it goes into our $_POST
variable and we'll create a new Update
function in our UserProfile
class.
- Design Principles for Process:driven Architectures Using Oracle BPM and SOA Suite 12c
- Vue.js入門與商城開發實戰
- AIRAndroid應用開發實戰
- Hands-On C++ Game Animation Programming
- Java性能權威指南(第2版)
- Learning ELK Stack
- Learning Data Mining with R
- GameMaker Essentials
- 零代碼實戰:企業級應用搭建與案例詳解
- Application Development with Parse using iOS SDK
- Python預測分析與機器學習
- Mastering Leap Motion
- SAP Web Dynpro for ABAP開發技術詳解:基礎應用
- C# 7.0本質論
- HTML5與CSS3權威指南