我要插入一些數據,是與JavaScript(科爾多瓦的應用程序)發送我送這個位置SQL INSERT查詢插入多次,不更新
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 ."')";
當我在PHP查詢之前呼應$產品名稱,我只看到productsnames(我想)
我的問題em是:它不會替換值,如果它已經存在,並且它插入多次,它只需插入一次,如果該列已經存在,則替換或更新。
有人可以幫助我嗎?在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
對不起,我的壞,我還有工作的答覆呢,它只是關於數據庫在線... –