Thumbnail
Category: PHP

Lệnh cơ bản thường sử dụng trong PHP

Date: May 18, 2020
16 views

1. Cơ sở dữ liệu

1.1 Kết nối


$servername = "localhost";
$username = "root";
$password = "";
$table = "quanlysinhvien";
​
// Create connection
$conn = new mysqli($servername, $username, $password, $table);
​
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

1.2 Truy vấn


$sql = "SELECT * FROM tbl_nganh";
​
$rs = $conn->query($sql);
​
if( $rs->num_rows > 0 )
{
  while( $row = $rs->fetch_assoc() ) {
    ?>
​
      <tr>
      <td><?php echo $row["maNganh"] ?></td>
      <td><?php echo$row["tenNganh"] ?></td>
      </tr>
​
      <?php
    }
}
​
$conn->close();
​

Nếu bị lỗi kết nối thì nhớ định nghĩa utf-8 khi truy vấn.


mysqli_set_charset($conn, 'UTF8');

2. Chuyển trang


header("Location: frm_them_nganh_hoc.php");

3. Upload file


<!-- form-upload.php -->
<!DOCTYPE html>
<html>
<body>
​
<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>
​
</body>
</html>


/* upload.php */
<?php
$target_dir = "uploads/";  //thay đổi thư mục cần lưu ảnh
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); //fileToUpload là name trong input file 
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
​
// Check if image file is a actual image or fake image
// kiểm tra file thật sự là hình ảnh
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); //tmp_name là tên file tạm bạn k cần phải thay thế
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}
​
// Check if file already exists
// Kiểm tra file đã tồn tại
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}
​
// Check file size
// Kiểm tra kích thước file
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}
​
// Allow certain file formats
// Các loại đuôi ảnh cho phép
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}
​
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { // move_uploaded_file là hàm up file lên server
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

Bạn chỉ cần copy hai nội dung vào hai file như ghi chú ở dòng 1 mỗi file. Riêng File thứ 2 thì bạn thay đổi các dòng cho phù hợp (3, 4, 37). Nếu thay đổi dòng 4 thì thay đổi thêm các dòng 11,30, 48, 49.


Copyright © 2025 All Right Reserved