gpt4 book ai didi

php - 数据没有使用 angularjs 中的 php 插入数据库

转载 作者:行者123 更新时间:2023-11-29 18:09:00 24 4
gpt4 key购买 nike

<!DOCTYPE html>
<html ng-controller="controller" ng-app="app">
username:<input type="text" placeholder="*username" ng-model="user"><br><BR>
password:<input type="password" placeholder="*password" ng-model="pass" ><br><BR>
Email:&emsp;<input type="email" placeholder="*email" ng-model="email"><br><BR>
<div align="center">
<input type="submit" value="register" ng-click="insert()">
<input type="button" value="Back" >
</div>


<script>
var app = angular.module("app", []);
app.controller("controller", function($scope, $http) {
$scope.insert = function() {
$http.post(
"insert.php", {
'user':$scope.user,
'pass':$scope.pass,
'email':$scope.email
}).then(function(response){
console.log("Data Inserted Successfully");
},function(error){
alert("Sorry! Data Couldn't be inserted!");
console.error(error);

});
}
});
</script>
</html>

.php

<?php
$conn = mysqli_connect("localhost", "root", "root", "students");
$info = json_decode(file_get_contents("php://input"),true);
if(count($info) > 0) {

$user = $data->user;
$pass = $data->pass;
$email=$data->email;

$query = "INSERT INTO students(username, password, email) VALUES('".$user."','".$pass."','".$email."')";

if(mysqli_query($conn, $query)) {
echo "Insert Data Successfully";
}
else {
echo "Failed";
}
}
?>

数据没有插入到表中,控制台上没有错误...代码中的错误是什么..浏览了很多视频但无法找到它..我正在使用 mysql workbench 6.3 v..任何人你能帮我吗

最佳答案

$data 变量未声明。它应该是 $info

<?php
$conn = mysqli_connect("localhost", "root", "root", "students");
$info = json_decode(file_get_contents("php://input"),true);
if(count($info) > 0) {

$user = $info->user;
$pass = $info->pass;
$email=$info->email;

$query = "INSERT INTO students(username, password, email) VALUES('".$user."','".$pass."','".$email."')";

if(mysqli_query($conn, $query)) {
echo "Insert Data Successfully";
}
else {
echo "Failed";
}
}
?>

关于php - 数据没有使用 angularjs 中的 php 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47551419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com