我正在尝试使用AngularJS
和PHP
创建一个简单的联系表单,该表单将发送到电子邮件和Google - big query database
,但没有成功。
而且似乎在任何地方都没有它的例子...... 爱得到任何帮助...
我展示了我要对MySQL
做什么的代码,我需要将其调整为bigquery
。
<body ng-app="myapp" ng-controller="formcontroller">
<div class="contact-us-page">
<div class="container">
<div class="row">
<div class="col-sm-4 col-xs-12">
<h1>CONTACT US</h1>
<form name="userForm" novalidate ng-submit="insertData()">
<label class="text-success" ng-show="successInsert">{{successInsert}}</label>
<div>
<label for="username" >Name</label>
<input type="text" name="userName" ng-model="insert.user_name" class="form-control" ng-class="{ 'has-error' : userForm.userName.$invalid && userForm.$submitted && userForm.userName.$pristine }" required>
<!--<span class="text-danger" ng-show="errorUsername">{{errorUsername}}</span>-->
</div>
<div>
<label for="companyName">Company name</label>
<input type="text" name="companyName" ng-model="insert.company_name" class="form-control" ng-class="{ 'has-error' : userForm.companyName.$invalid && userForm.$submitted && userForm.companyName.$pristine }" required>
<!--<span class="text-danger" ng-show="errorCompanyname">{{errorCompanyname}}</span>-->
</div>
<div>
<label for="email">Email</label>
<input type="email" name="email" ng-model="insert.email" class="form-control" ng-class="{ 'has-error' : userForm.email.$invalid && userForm.$submitted && userForm.email.$pristine }" required>
<!--<span class="text-danger" ng-show="errorEmail">{{errorEmail}}</span>-->
</div>
<div>
<label for="phone">Phone</label>
<input type="text" name="phone" ng-model="insert.phone" class="form-control" ng-class="{ 'has-error' : userForm.phone.$invalid && userForm.$submitted && userForm.phone.$pristine }" required>
<!--<span class="text-danger" ng-show="errorPhone">{{errorPhone}}</span>-->
</div>
<!--<div>
<input type="file" name="upload_cv" value="Upload CV" class="upload-cv">
</div> -->
<input type="submit" name="insert" class="button-style" value="Send">
</form>
</div>
</div>
</div>
</div>
<script src="app.js"></script>
</body>
app.js:
var application = angular.module("myapp", []);
application.controller("formcontroller", function($scope, $http){
$scope.insert = {};
$scope.insertData = function(){
$http({
method:"POST",
url:"insert.php",
data:$scope.insert,
}).success(function(data){
if(data.error)
{
$scope.errorUsername = data.error.user_name;
$scope.errorCompanyname = data.error.company_name;
$scope.errorEmail = data.error.email;
$scope.errorPhone = data.error.phone;
$scope.successInsert = null;
}
else
{
$scope.insert = null;
$scope.errorUsername = null;
$scope.errorCompanyname = null;
$scope.errorEmail = null;
$scope.errorPhone = null;
$scope.successInsert = data.message;
}
});
}
});
insert.php:
<?php
//insert.php
$connect = mysqli_connect("localhost", "root", "", "resonai");
$form_data = json_decode(file_get_contents("php://input"));
$data = array();
$error = array();
if(empty($form_data->user_name))
{
$error["user_name"] = "User Name is required";
}
if(empty($form_data->company_name))
{
$error["company_name"] = "Company Name is required";
}
if(empty($form_data->email))
{
$error["email"] = "Email Name is required";
}
if(empty($form_data->phone))
{
$error["phone"] = "Phone Name is required";
}
if(!empty($error))
{
$data["error"] = $error;
}
else
{
$user_name = mysqli_real_escape_string($connect, $form_data->user_name);
$company_name = mysqli_real_escape_string($connect, $form_data->company_name);
$email = mysqli_real_escape_string($connect, $form_data->email);
$phone = mysqli_real_escape_string($connect, $form_data->phone);
$query = "
INSERT INTO contactus_page(user_name, company_name, email, phone) VALUES ('$user_name', '$company_name', '$email', '$phone')
";
if(mysqli_query($connect, $query))
{
$data["message"] = "Thank you. We will contact you soon!";
}
}
echo json_encode($data);
?>
insert.php with bigquery- not working ...
<?php
//insert.php
composer require google/cloud-bigquery;
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\BigQuery\BigQueryClient;
$projectId = 'burnished-data-183409';
$form_data = json_decode(file_get_contents("php://input"));
$data = array();
$error = array();
if(empty($form_data->user_name))
{
$error["user_name"] = "User Name is required";
}
if(empty($form_data->company_name))
{
$error["company_name"] = "Company Name is required";
}
if(empty($form_data->email))
{
$error["email"] = "Email Name is required";
}
if(empty($form_data->phone))
{
$error["phone"] = "Phone Name is required";
}
if(!empty($error))
{
$data["error"] = $error;
}
else
{
# [START build_service]
$bigquery = new BigQueryClient([
'projectId' => $projectId
]);
# [END build_service]
# [START run_query]
$user_name = $form_data->user_name;
$company_name = $form_data->company_name;
$email = $form_data->email;
$phone = $form_data->phone;
$query = 'insert into test1.cf (user_name, company_name, email, phone) values ('$user_name', '$company_name', '$email', '$phone')';
$options = ['useLegacySql' => true];
$queryResults = $bigQuery->runQuery($query, $options);
# [END run_query]
# [START print_results]
if ($queryResults->isComplete()) {
$data["message"] = "Thank you. We will contact you soon!";
/*$i = 0;
$rows = $queryResults->rows();
foreach ($rows as $row) {
printf('--- Row %s ---' . PHP_EOL, ++$i);
foreach ($row as $column => $value) {
printf('%s: %s' . PHP_EOL, $column, $value);
}
}
printf('Found %s row(s)' . PHP_EOL, $i);*/
} else {
throw new Exception('The query failed to complete');
}
# [END print_results]
# [END all]
}
echo json_encode($data);
?>
答案 0 :(得分:0)
我想你正在尝试在BigQuery上执行当前正在MySQL上执行的查询。对于该用例,您应该使用BigQuery PHP客户端库。有关其安装和使用的更多信息,请访问https://cloud.google.com/bigquery/create-simple-app-api [单击PHP选项卡]