Я хочу, чтобы вставить некоторые данные, которые являются отправить с JavaScript (Cordova приложением) я отправить это здесьSQL вставка вставка запрос несколько раз и не обновляет
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
}
}
});
}
}
});
Теперь я хочу, чтобы добавить это в моих базах данных онлайн (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 ."')";
Когда я эхо $ ProductName до запроса в PHP, я просто увидеть productsnames (что я хочу)
Моя проблема em: это не заменяет значение, если оно уже существует, и оно вставляет несколько раз, оно должно вставлять только один раз и заменять или обновлять, если столбец уже существует.
Может кто-нибудь помочь мне?
UPDATE запрос в 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);
}
Если я прав, 'userID' является Первичным ключом таблицы' card', и вы не передаете 'userID' в запрос' REPLACE'. – antorqs
извините, мое плохое, я уже работаю над обработкой, это просто о базах данных онлайн ... –