I want to insert some data that's is send with javascript (cordova app) i send this here
我想插入一些用javascript(cordova应用程序)发送的数据我在这里发送
var link = 'somelink';
var stmt = "SELECT * FROM card WHERE Productsync = 0 ";
//console.log(stmt);
$cordovaSQLite.execute(db, stmt).then(function(res) { //console.log(res)
if(res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++){
console.log(res.rows.item(i));
console.log(res.rows.item(i).ProductName);
console.log(res.rows.item(i).ProductBarcode);
console.log(res.rows.item(i).ProductPakking);
console.log(res.rows.item(i).ProductPresent);
console.log(res.rows.item(i).ProductMinimuim);
console.log(res.rows.item(i).FotoUrl);
console.log(res.rows.item(i).DBid);
$http.post(link, {ProductName : res.rows.item(i).ProductName , ProductBarcode : res.rows.item(i).ProductBarcode , ProductPakking : res.rows.item(i).ProductPakking , ProductPresent : res.rows.item(i).ProductPresent , ProductMinimuim : res.rows.item(i).ProductMinimuim , FotoUrl : res.rows.item(i).FotoUrl , DBid : res.rows.item(i).DBid}).then(function (res){//sending data
console.log(res.data);//respone of server
var data = res.data;
for (var key in data) {//read out respone
if (data.hasOwnProperty(key)) {
var obj = data[key];
console.log(obj);
//have to make this
}
}
});
}
}
});
Now i want it to add this in my databases online (BIND variables will i do later) This is how my php script looks like
现在我希望它在我的数据库中在线添加它(BIND变量我将在以后做)这是我的PHP脚本的样子
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
$ProductName = $request->ProductName;
$ProductBarcode = $request->ProductBarcode;
$ProductPakking = $request->ProductPakking;
$ProductPresent = $request->ProductPresent;
$ProductMinimuim = $request->ProductMinimuim;
$FotoUrl = $request->FotoUrl;
$DBid = $request->DBid;
$ProductName = mysqli_real_escape_string($connection,$ProductName);
$ProductBarcode = mysqli_real_escape_string($connection,$ProductBarcode);
$ProductPakking = mysqli_real_escape_string($connection,$ProductPakking);
$ProductPresent = mysqli_real_escape_string($connection,$ProductPresent);
$ProductMinimuim = mysqli_real_escape_string($connection,$ProductMinimuim);
$FotoUrl = mysqli_real_escape_string($connection,$FotoUrl);
$DBid = mysqli_real_escape_string($connection,$DBid);
$query ="REPLACE into `card` (ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
VALUES( '" . $ProductName . "' ,
'" . $ProductBarcode . "' ,
'" . $ProductPakking . "' ,
'" . $ProductPresent . "' ,
'" . $ProductMinimuim . "' ,
'" . $FotoUrl . "' ,
'" . $DBid ."')";
When I echo the $ProductName before the query in php, i just see the productsnames (what I want)
当我在php中查询之前回显$ ProductName时,我只看到产品名称(我想要的)
My problem is: it don't replace the value if it already exists, and it insert multiple times, it have to insert just one time and replace or update if the column already exists.
我的问题是:它不会替换值,如果它已经存在,并且它插入多次,它必须只插入一次,如果列已经存在则替换或更新。
Can someone help my ?
有人可以帮我吗?
UPDATE QUERY IN PHP
用PHP更新查询
$query ="SELECT ProductName FROM `card` WHERE
`ProductName` = '" . $ProductName . "' AND
`ProductBarcode` = '" . $ProductBarcode . "' AND
`ProductPakking` = '" . $ProductPakking . "' AND
`ProductPresent` = '" . $ProductPresent . "' AND
`ProductMinimuim` = '" . $ProductMinimuim . "' AND
`FotoUrl` = '" . $FotoUrl . "' AND
`DBid` = '" . $DBid ."'";
$result_set = mysqli_query($connection,$query);
if (mysqli_num_rows($result_set) == 0){
$query ="INSERT INTO `card` (ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
VALUES( '" . $ProductName . "' ,
'" . $ProductBarcode . "' ,
'" . $ProductPakking . "' ,
'" . $ProductPresent . "' ,
'" . $ProductMinimuim . "' ,
'" . $FotoUrl . "' ,
'" . $DBid ."')";
$result = mysqli_query($connection,$query);
}else{
$query = "UPDATE `card`
SET `ProductName` = '" . $ProductName . "' ,
`ProductBarcode` = '" . $ProductBarcode . "' ,
`ProductPakking` = '" . $ProductPakking . "' ,
`ProductPresent` = '" . $ProductPresent . "' ,
`ProductMinimuim` = '" . $ProductMinimuim . "' ,
`FotoUrl` = '" . $FotoUrl . "' ,
`DBid` = '" . $DBid ."'
WHERE `ProductBarcode` = '" . $ProductBarcode . "' AND `DBid` = '" . $DBid ."'";
$fines = mysqli_query($connection,$query);
}
2 个解决方案
#1
0
Note that for this approach (or the REPLACE INTO approach or even the ON DUPLICATE KEY approach) to be successfull you need to have a unique index on the fields that you don't want duplicated.
请注意,对于此方法(或REPLACE INTO方法甚至ON DUPLICATE KEY方法)要成功,您需要在不希望重复的字段上具有唯一索引。
From docs states
来自docs状态
REPLACE makes sense only if a table has a PRIMARY KEY or UNIQUE index. Otherwise, it becomes equivalent to INSERT, because there is no index to be used to determine whether a new row duplicates another.
只有当表具有PRIMARY KEY或UNIQUE索引时,REPLACE才有意义。否则,它变得等同于INSERT,因为没有索引可用于确定新行是否与另一行重复。
#2
0
looks like you have some issue with below lines
看起来你在下面的行有一些问题
console.log(res.data);//respone of server
var data = res.data;
for (var key in data) {
this loop has some problem because if it json then it iterate over all key one by one and it insert multiple of keys record keys injson * number of time response
这个循环有一些问题,因为如果它json然后它逐个迭代所有键,它插入多个键记录键injson *时间响应的数量
please provide res.data logs and also check what you get in key and how many time you looping around that loop.
请提供res.data日志,并检查您获得的密钥以及循环该循环的时间。
#1
0
Note that for this approach (or the REPLACE INTO approach or even the ON DUPLICATE KEY approach) to be successfull you need to have a unique index on the fields that you don't want duplicated.
请注意,对于此方法(或REPLACE INTO方法甚至ON DUPLICATE KEY方法)要成功,您需要在不希望重复的字段上具有唯一索引。
From docs states
来自docs状态
REPLACE makes sense only if a table has a PRIMARY KEY or UNIQUE index. Otherwise, it becomes equivalent to INSERT, because there is no index to be used to determine whether a new row duplicates another.
只有当表具有PRIMARY KEY或UNIQUE索引时,REPLACE才有意义。否则,它变得等同于INSERT,因为没有索引可用于确定新行是否与另一行重复。
#2
0
looks like you have some issue with below lines
看起来你在下面的行有一些问题
console.log(res.data);//respone of server
var data = res.data;
for (var key in data) {
this loop has some problem because if it json then it iterate over all key one by one and it insert multiple of keys record keys injson * number of time response
这个循环有一些问题,因为如果它json然后它逐个迭代所有键,它插入多个键记录键injson *时间响应的数量
please provide res.data logs and also check what you get in key and how many time you looping around that loop.
请提供res.data日志,并检查您获得的密钥以及循环该循环的时间。