美好的一天,我是PHP的新手,我真的需要一些帮助。我在插入数据后试图在新页面中发出确认消息。我试着回声并打印出结果,但对我来说没什么用!!!!这是我遇到的唯一一个我无法解决的问题!!!
request.php 页面的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body dir="rtl">
<div class="container" style="width:400px;">
<div ng-app="myapp" ng-controller="formcontroller">
<form name="userForm" id="contact" ng-submit="insertData()">
<label class="text-success" ng-show="successInsert">{{successInsert}}</label>
<div class="form-group">
<label>name<span class="text-danger"></span>
</label>
<input type="text" name="name" ng-model="insert.name" class="form-control" /><span class="text-danger" ng-show="errorname">{{errorname}}</span>
</div>
<fieldset class="frmain">
<label>contact</label>
<div class="form-group">
<input type="text" name="em" ng-model="insert.em" class="form-control" placeholder="اemail" />
<span class="text-danger" ng-show="errorem">{{errorem}}</span>
<input type="text" name="telph" ng-model="insert.telph" class="form-control" placeholder="mobile" />
<span class="text-danger" ng-show="errortelph">{{errortelph}}</span>
</div>
<div class="form-group">
<label>department<span class="text-danger"></span>
</label>
<select ng-model="insert.dept" name="dept" class="form-control" style="font-family: times new roman;font-size: 18px;" />
<option value=""></option>
<option value="accounting department" </option>
<option value="huamn resources"></option>
<option value="IT department"></option>
</select>
<span class="text-danger" ng-show="errordept">{{errordept}}</span>
</div>
<div class="form-group" style="width: 320px;">
<label>details<span class="text-danger"></span>
</label>
<textarea name="det" ng-model="insert.det" class="form-control"></textarea>
<span class="text-danger" ng-show="errordet">{{errordet}}</span>
</div>
<div class="form-group">
<button name="submit" type="submit">send</button>
</div>
</form>
</div>
</div>
</body>
</html>
<script>
var application = angular.module("myapp", []);
application.controller("formcontroller", function($scope, $http) {
$scope.insert = {};
$scope.insertData = function() {
$http({
method: "POST",
url: "req_add.php",
data: $scope.insert,
}).success(function(data) {
if (data.error) {
$scope.errorname = data.error.name;
$scope.errorem = data.error.em;
$scope.errortelph = data.error.telph;
$scope.errordept = data.error.dept;
$scope.errordet = data.error.det;
$scope.successInsert = null;
} else {
$scope.insert = null;
$scope.errorname = null;
$scope.errorem = null;
$scope.errortelph = null;
$scope.errordept = null;
$scope.errordet = null;
$scope.successInsert = data.message;
}
});
}
});
</script>
req_add.php 页面的代码:(将数据插入数据库)
<?php
//req_add.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$form_data = json_decode(file_get_contents("php://input"));
mysqli_query($connect,'set names utf8') or die (mysqli_error($connect));
$data = array();
$error = array();
$date1 = date('Y/m/d H:i:s');// 2017-07-12 10:35:45
$today = date("Ymd");
$rand = strtoupper(substr(uniqid(sha1(time())),0,4));
$no1 = $today . $rand;
if(empty($form_data->name))
{
$error["name"] = "name is required";
}
if(empty($form_data->em))
{
$error["em"] = "email is required";
}
if(empty($form_data->telph))
{
$error["telph"] = "mobile number is required";
}
if(empty($form_data->dept))
{
$error["dept"] = "department is required";
}
if(empty($form_data->det))
{
$error["det"] = "details are required";
}
if(!empty($error))
{
$data["error"] = $error;
}
else
{
$name = mysqli_real_escape_string($connect, $form_data->name);
$em = mysqli_real_escape_string($connect, $form_data->em);
$telph = mysqli_real_escape_string($connect, $form_data->telph);
$dept = mysqli_real_escape_string($connect, $form_data->dept);
$det = mysqli_real_escape_string($connect, $form_data->det);
$no = mysqli_real_escape_string($connect, $form_data->no1);
$reqdate = mysqli_real_escape_string($connect, $form_data->reqdate);
$answer = mysqli_real_escape_string($connect, $form_data->answer);
$query = "INSERT INTO requests(name, em, telph, dept, det, no, reqdate, answer) VALUES ('$name','$em', '$telph', '$dept', '$det', '$no1', '$date1', 'no answer')";
if(mysqli_query($connect, $query))
{
$data["message"] = "thank you for your request, your request nymber is: " .$no1;
}
}
echo json_encode($data);
?>
答案 0 :(得分:0)
一个有用的选项是使用mysqli_insert_id
来插入最后一个ID
此函数获取DB中的最后一个id,不仅在insert表中,而且足够了。
另一种是手动形式,查询返回自定义值 在您的代码中:
$query ="INSERT INTO requests(name, em, telph, dept, det, no, reqdate,answer)
VALUES ('$name','$em', '$telph', '$dept', '$det', '$no1','$date1', 'no answer')";
if(mysqli_query($connect, $query))
{
$data["message"] = "thank you for your request, your request nymber is: " .$no1;
}
}
echo json_encode($data);
添加此更改:
$query = "INSERT INTO requests(name, em, telph, dept, det, no, reqdate, answer)
VALUES ('$name','$em', '$telph', '$dept', '$det', '$no1', '$date1', 'no answer')";
$query .="; select 1 as insert_ok;";
if($stmt=mysqli_query($connect, $query)) // Get result
{
$fetch = mysql_fetch_object($stmt);
if ($fetch->insert_ok) {
$data["message"] = "thank you for your request, your request nymber is: " .$no1;
}else{
$data["message"] = "The register wasn't inserted";
} // else
} //if $stmt
echo json_encode($data);
在您的脚本代码中:验证从php脚本返回的insert_ok值。