官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 汤原县| 赤水市| 翁牛特旗| 泰兴市| 务川| 惠来县| 陵水| 兰溪市| 改则县| 乌兰浩特市| 西乌珠穆沁旗| 故城县| 漳州市| 崇仁县| 常德市| 澄迈县| 高陵县| 乃东县| 西乌| 东光县| 曲阳县| 珲春市| 奉新县| 宁海县| 东乡县| 建宁县| 青岛市| 嵩明县| 富顺县| 修武县| 扬中市| 顺平县| 东乡| 银川市| 西吉县| 德庆县| 沁阳市| 河间市| 营山县| 嘉祥县| 玉屏|