- 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.
- Web程序設(shè)計(jì)及應(yīng)用
- 精通Nginx(第2版)
- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門寶典
- 零基礎(chǔ)搭建量化投資系統(tǒng):以Python為工具
- Java異步編程實(shí)戰(zhàn)
- Photoshop智能手機(jī)APP UI設(shè)計(jì)之道
- 基于Java技術(shù)的Web應(yīng)用開發(fā)
- Linux環(huán)境編程:從應(yīng)用到內(nèi)核
- Python數(shù)據(jù)結(jié)構(gòu)與算法(視頻教學(xué)版)
- Mastering ROS for Robotics Programming
- Mastering Elixir
- C++從入門到精通(第6版)
- JSP程序設(shè)計(jì)與案例實(shí)戰(zhàn)(慕課版)
- Learning Kotlin by building Android Applications
- Mastering OAuth 2.0