[英]Cannot save data in SQL using AngularJS and PHP
i have this code that saves a data to sql using angularJS and PHP. 我有这段代码,使用angularJS和PHP将数据保存到sql。 Somehow, i cannot save the data to my database. 不知何故,我无法将数据保存到数据库中。 The angularJS code seems right and it says that its successfully saved but when i checked my database, it has nothing. angularJS代码似乎正确,它表示已成功保存,但是当我检查数据库时,它什么也没有。 So im guessing that i have the wrong code in my php. 所以我猜我在我的PHP中有错误的代码。 Ive done this and that but i cannot find the solution. 我已经做到了这一点,但我找不到解决方案。 btw im using intel xdk. 顺便说一句,即时通讯使用Intel XDK。 here is my code. 这是我的代码。
IntelXDK 英特尔XDK
<body ng-app="kelvinApp">
<div class="upage vertical-col left" id="mainpage">
<form name="fileupload" id="fileupload" ng-controller="mainController">
<div class="widget uib_w_1 scale-image d-margins" data-uib="media/img" data-ver="0">
<figure class="figure-align">
<img src="images/Checkmark.png" id="img_display">
<figcaption data-position="bottom"></figcaption>
</figure>
</div>
<div class="widget uib_w_2 d-margins" data-uib="media/file_input" data-ver="0">
<input type="file" accept="image/jpeg, image/png, image/gif" id="img_path" name="img_path" ng-model="image_path">
</div>
<label class="item item-input widget uib_w_3 d-margins" data-uib="ionic/input" data-ver="0" id="image_id"><span class="input-label">ID:</span>
<input type="text" name="image_id" ng-model="image_id">
</label>
<label class="item item-input widget uib_w_4 d-margins" data-uib="ionic/input" data-ver="0" id="image_name"><span class="input-label">Image Name:</span>
<input type="text" name="image_name" ng-model="image_name">
</label>
<label class="item item-input widget uib_w_5 d-margins" data-uib="ionic/input" data-ver="0" id="image_title"><span class="input-label">Image Title:</span>
<input type="text" name="image_title" ng-model="image_title">
</label>
<button class="button widget uib_w_7 d-margins" data-uib="ionic/button" data-ver="0" id="btn_upload" name="btn_upload" ng-click="uploadfile()">Upload</button>
</form>
</div>
</body>
</html>
app.js (angularJS) app.js(angularJS)
var app = angular.module('kelvinApp', ['ionic']);
app.controller('mainController', function($scope, $http){
$scope.uploadfile = function() {
$http.post('http://localhost/Project/addfile.php',
{
'image_path' : $scope.image_path,
'image_name' : $scope.image_name,
'image_title' : $scope.image_title
})
.success(function(data, status, headers, config) {
alert("Success!");
})
.error(function(data, status, headers, config) {
alert("Sorry, Try Again!");
})
}
});
and here is my PHP code, i guess this is where i am wrong. 这是我的PHP代码,我想这是我错的地方。 the data isnt saving to database. 数据不保存到数据库。
<?php
require('db.php');
if(isset($_POST['submit'])){
global $conn;
$data = json_decode(file_get_contents("php://input"));
$image_path = mysql_real_escape_string($data->image_path);
$image_name = mysql_real_escape_string($data->image_name);
$image_title = mysql_real_escape_string($data->image_title);
print_r($data);
$query = $conn->prepare("INSERT INTO projectsql.tbl_imgstorage(image_name, image_title, image_path) VALUES(?,?,?)");
console.log("2");
$result = $query->execute(array($image_name, $image_title, image_path));
if($result){
$array = array('msg' => "Added Successfully", 'error' => '');
$json= json_encode($array);
print_r($json);
} else {
$array = array('msg' => "", 'error' => 'Error in inserting record');
$json = json_encode($array);
print_r($json);
}
}
?>
I have to to be honest here; 我必须在这里诚实。 I don't know anything about angularjs or intel xdk, but I do know PHP and MySQL and can say this: 我对angularjs或intel xdk一无所知,但我确实了解PHP和MySQL,可以这样说:
You don't have an input element holding the submit
name attribute so everything inside if(isset($_POST['submit'])){...}
won't execute. 您没有包含submit
名称属性的输入元素,因此if(isset($_POST['submit'])){...}
都不会执行。
Therefore you will need to rename your submit button from name="btn_upload"
to name="submit"
因此,您需要将提交按钮从name="btn_upload"
重命名为name="submit"
Then uploading files requires a valid enctype such as enctype="multipart/form-data"
. 然后上传文件需要有效的enctype,例如enctype="multipart/form-data"
。
Then you're using mysqli_
syntax but using a mysql_
function such as mysql_real_escape_string()
which does not mix with the mysqli_
API. 然后,您将使用mysqli_
语法,但使用的mysql_
函数(例如mysql_real_escape_string()
不会与mysqli_
API混合使用。
It needs to be mysqli_real_escape_string()
while passing DB connection to it as the first parameter. 在将数据库连接作为第一个参数传递给它时,它必须是mysqli_real_escape_string()
。
It is unclear though as to which API you are using to connect with and what is inside require('db.php');
目前尚不清楚您要使用哪个API进行连接以及require('db.php');
内部有什么内容require('db.php');
. 。
Seeing your query $query = $conn->prepare
suggests either mysqli_
or PDO. 看到查询$query = $conn->prepare
建议使用mysqli_
或PDO。
You need to use the same MySQL API from connection to querying. 从连接到查询,您需要使用相同的MySQL API。
References: 参考文献:
Sidenote: 边注:
I don't know what this function does ng-click="uploadfile()"
, if it's contributing to the failure of your code or not, as it isn't posted in your question. 我不知道ng-click="uploadfile()"
这个函数是做什么的,如果它导致您的代码失败,或者它是否会引起您的代码失败,因为它没有发布在您的问题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.