我正在尝试将我的后端数据库mysql连接到我的应用程序,但由于我不了解php而无法做到并得到。我希望在登录时从数据库中检查我的用户名和密码,我自己尝试过但无法获得,请发布带有很少解释的解决方案
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'login-form',
templateUrl: './login-form.component.html',
styleUrls: ['./login-form.component.css']
})
export class LoginFormComponent implements OnInit {
username: string;
password: string;
form: FormGroup;
constructor(private router: Router, private formBuilder: FormBuilder) {
}
ngOnInit() {
this.initf();
}
initf() {
this.form = this.formBuilder.group({
username: this.username,
password: this.password
});
}
log() {
console.log(this.username, this.password);
if ( this.username === 'admin' && this.password === 'admin') {
this.router.navigate(['todoapp']);
}
}
}
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
if(!isset($_POST)) die();
session_start();
$response = [];
$con = mysqli_connect('localhost', 'root', '', 'test_site');
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$query = "SELECT * FROM `users` WHERE username='$username' AND password='$password'";
$result = mysqli_query($con, $query);
echo json_encode($response);
答案 0 :(得分:0)
我的朋友,解释的时间更长了一点,但是我可以告诉你,您可以按照本教程的有关如何将PHP和angular进行连接
至此,您的后端已在PHP中 https://www.techiediaries.com/php-angular/
,但是您需要遵循第二步,以了解如何使用PHP构建,结构化Angular组件和服务 https://www.techiediaries.com/php-angular-crud-api-httpclient-forms/
希望这会有所帮助